FRANKFORT, Ky. (AP) — The Kentucky Supreme Court on Thursday struck down a law that made changes to one of the country’s worst-funded public pension systems, a victory for teachers who shut down schools across the state to protest the law earlier this year.

Democratic Attorney General Andy Beshear, who filed the lawsuit that led to Thursday’s ruling, called it “a landmark win for all of our public servants.” But Republican Gov. Matt Bevin called it “an unprecedented power grab by activist judges.

“This will destroy the financial condition of Kentucky,” Bevin said, a claim Beshear dismissed as “fear mongering.”

Public pension systems across the country are in trouble as workers live longer and states grapple to make up investment losses from the Great Recession. But Kentucky’s pension systems are considered the worst of the worst, with the state at least $38 billion short of the money it needs to pay benefits over the next three decades. The shortfall has required state lawmakers to divert billions of dollars in state money to the pension system, leaving little else for other services like education and health care.

In April, Gov. Bevin signed a law that moved all new teacher hires into a hybrid pension plan. The law also restricted how teachers used sick days to calculate their retirement benefits and changed how the state pays off its pension debt.

Facing a tight deadline, state lawmakers introduced and passed the bill in one day near the end of the 2018 legislative session. The bill moved so quickly that a copy was not available for the public to read until the day after lawmakers had voted on it.

Teachers were outraged, thousands marched on the Capitol and schools in more than 30 districts closed. Beshear sued, arguing the legislature violated the state Constitution by not voting on the proposal three times over three separate days. Bevin argued lawmakers did not need to do that because they had substituted the bill for an unrelated one that already had the required votes.

Thursday, the state’s highest court sided with Beshear. It ruled that lawmakers cannot take a bill close to final passage and replace it with an unrelated bill without voting on it three times over three separate days as the Constitution requires.

The ruling could have political consequences. Bevin is up for re-election in 2019, and Beshear is one of the Democratic candidates vying to replace him. The two men have clashed in court multiple times since 2015.

It’s unclear how the legislature will respond. Lawmakers are scheduled to reconvene in January, and they could opt to pass the bill again without using the legislative maneuver the court ruled unconstitutional. Republican leaders in the state House of Representatives released a statement Thursday saying they were committed “to enact a solution” for the pension system. But they warned the court’s ruling could have far-reaching consequences.

“This unfortunate decision now subjects decades of good legislation to potential legal challenges, while revealing a complete and total lack of understanding for the separation of powers,” the statement read.

The justices considered that argument, and rejected it. Justice Daniel Venters wrote “we are not persuaded from the record here that such a potential parade of horrors awaits.”

Democrats had hoped the backlash on the pension vote would give their candidates a boost in the November elections. But on Election Day, more lawmakers who opposed the pension bill lost than did those who supported it. Democrats made modest gains in the House, but Republicans maintained substantial majorities in both chambers.

The Kentucky Democratic Party viewed Thursday’s ruling as a momentum swing.

“The Kentucky Democratic Party intends to hold (Republican Gov. Matt Bevin) accountable in 2019 and make him a one-term governor,” Kentucky Democratic Party chairman Ben Self said.

Bevin’s office did not immediately respond to a request for comment.

Meanwhile, the Kentucky Teachers’ Retirement System is improving. Independent consultants said last month that the system has more money and less unfunded debt since the legislature decided to fully fund the system’s budget request. Kentucky Teachers’ Retirement System Executive Secretary Gary Harbin told reporters the fate of the pension bill would have little effect on the system’s future solvency.