WASHINGTON (AP)– The Supreme Court on Monday ruled for doctors who face criminal charges for overprescribing powerful pain medication in a case arising from the opioid addiction crisis.

Justice Stephen Breyer wrote for the court that prosecutors must prove that doctors knew they were illegally prescribing powerful pain drugs in violation of the federal Controlled Substances Act.

The ruling came as the U.S. has been seeing record numbers of drug overdose deaths, many from the highly lethal opioid fentanyl.

Evaluating the convictions of two doctors who are each facing more than two decades in prison, the justices ruled on a subject on which advocates for patients and doctors had urged the court to distinguish between criminal behavior and medical errors made in good faith.

It did so in the ruling. Prosecutors, Breyer wrote, “must prove beyond a reasonable doubt that the defendant knowingly or intentionally acted in an unauthorized manner.”

Fear of aggressive prosecution already has led doctors to avoid prescribing opioids “against their best medical judgment,” the National Pain Advocacy Center told the court in a written filing.

But the justices did not throw out the convictions of two doctors whose appeal was heard in February. Instead, it ordered federal appeals courts to take a new look at their cases.

The court ruled on appeals from Xiulu Ruan of Mobile, Alabama, and Shakeel Kahn, who practiced medicine in Ft. Mohave, Arizona, and Casper, Wyoming.

Ruan is serving a 21-year federal prison term. Kahn is in prison for up to 25 years. They will get another chance to argue that their convictions should be overturned.

Ruan and a partner, James Couch, were convicted of overprescribing medications at their Physicians Pain Specialists of Alabama clinic and a pharmacy.

Kahn was convicted of conspiracy to unlawfully distribute and dispense controlled substances resulting in death, including oxycodone, an opioid pain reliever, and fentanyl, a synthetic opioid.

Jessica Burch, of Lake Havasu City, Arizona, was a patient of Kahn’s who died from an overdose in 2015.