An Alabama law barring teachers from having sex with their students was ruled unconstitutional Thursday by a state judge who also dismissed charges against two instructors who were facing 20 years behind bars for sleeping with students.

Judge Glenn Thompson dismissed charges against a former high school teacher, Carrie Witt, 44, and David Solomon, 27, a former aide at a different school.

Under state law, school employees “engaging in a sex act or deviant sexual intercourse with a student,” even if consensual, face not only possible jail time but also a lifetime on the sex offender registry.

Witt was accused of sleeping with two students, aged 17 and 18, respectively. Solomon was accused of having sex with a 17-year-old. But their lawyers argued that Alabama’s law — passed in 2010 — violates teachers’ equal protection rights guaranteed under the 14th Amendment, because it treats educators differently under the law than other citizens.

For example, other adults having consensual sex with teenagers over 16, the age of consent, don’t face criminal charges. On the flip side, prosecutors say that because teachers have special disciplinary and authoritative powers, sexual relations with students is inherently coercive, and the law is designed to protect students in an educational environment.

But the existing statute doesn’t account for nuances in the student-teacher relationship, according to the judge, AL.com reported. Thompson referred to other state laws, including in Texas, Arkansas and Kansas, which specifically examine whether a teacher abused their position of power in their sexual relations with students.

Prosecutors plan to appeal Thompson’s ruling, local media reported, and next stop for the case will likely be the Alabama Criminal Court of Appeals. Whoever loses in that court can appeal to the State Supreme Court to either safeguard the law, or have it amended.

Other states’ laws criminalizing sex between students and teachers have also fallen under legal scrutiny. In 2012, Arkansas’ Supreme Court ruled that students over the age of 18 had a constitutional right to engage in a consensual sexual relationship with their teacher.