A Western New York doctor accused of prescribing pain pills that eventually caused some of his patients to die went before a judge Tuesday.

Dr. Eugene Gosy pleaded not guilty to all charges in the 166-count indictment he’s facing.

He is accused of unlawfully prescribing controlled substances that resulted in the death of six of his patients.

Gosy, who operates Gosy pain management in Williamsville, was indicted on 114 counts in April 2016.

At the time, as a part of his bail agreement, the judge allowed him to continue practicing medicine under the supervision of another physician at his clinic.

Tuesday, the prosecution motioned to have that provision changed, arguing Gosy still posed a threat to his patients.

That motion was denied because the judge ruled that there is no evidence that Gosy violated the terms of his bail.

Gosy is facing a minimum of 20 years behind bars if found guilty.

Oral arguments will begin March 19.