(CNN) -- A Montana teacher who served 31 days in jail for raping a 14-year-old girl who later killed herself must be re-sentenced, the Montana Supreme Court ordered Wednesday.
The court ruled that Stacey Dean Rambold's original sentence was inadequate and outside legislative guidelines, which it said should have ensured Rambold spend at least two years behind bars.
The justices also barred Judge G. Todd Baugh, who drew a firestorm of criticism for levying the original sentence last year, from imposing the new sentence, saying a new judge was needed "to preserve the appearance of fairness and justice."
Authorities said Rambold raped Cherise Morales, one of his students at a Billings, Montana, high school, in 2007. Morales committed suicide in February 2010, before the case went to trial.
Rambold pleaded guilty to sexual intercourse without consent, and last year Baugh sentenced him to 15 years in prison, with all but 31 days suspended. That meant serving 31 days in jail and more than 14 years on probation.
Baugh's comments at the sentencing in August sparked controversy. He said the girl looked older than her years and was "probably as much in control of the situation as was the defendant," according to the Montana attorney general's office, which appealed the sentence.
In Wednesday's decision, the state Supreme Court ruled that Baugh used an inapplicable statute to impose the 31-day sentence. When read properly, Montana law mandates a minimum four-year prison sentence -- with a suspension of no more than two years -- for the rape of children under 16 by someone at least three years older, the court ruled.
Explaining its decision to give the new sentence to a different judge, the court blasted Baugh's previous comments about the case.
At the August sentencing, Baugh said the victim looked older than her years and was "probably as much in control of the situation as was the defendant," according to the Montana attorney general's office.
Baugh's comments reflected an improper bias and "cast serious doubt on the appearance of justice," state Supreme Court Justice Michael E. Wheat wrote in Wednesday's ruling.
Baugh apologized for the comments in November. Writing to a judicial review board, he said: "I am sorry I made those remarks. They focused on the victim when that aspect of the case should have been focused on the defendant."
But he defended the overall decision in December, calling it "the right kind of sentence."
The review board filed a complaint against Baugh in February, saying his actions in the Rambold case "eroded public confidence" in the judicial system. In Wednesday's ruling, Wheat wrote that the state Supreme Court would address the complaint in a separate hearing.
Baugh told CNN affiliate KTVQ in January that he would retire at the end of the year, and that his decision wasn't related to the Rambold controversy.
Authorities said Rambold had sexual relations with Morales in fall 2007, when she was 14 and a student in one of his classes at Billings Senior High. She confided in a church group leader, and Rambold was charged in October 2008.
CNN's Amanda Watts, Steve Almasy and Ed Payne contributed to this report.