WARSAW, Poland (AP) — Poland’s Supreme Court on Tuesday overturned a contentious presidential pardon of a top government official and three subordinates and ordered a retrial of their case.

The surprise ruling in the years-long legal dispute surrounding the pardon went against the interests of the governing nationalist party. As such, it was a rare act of independence from a top court after years of efforts by the Law and Justice party and President Andrzej Duda to establish greater control over the judiciary.

An aide to Duda criticized the court’s ruling, arguing that it didn’t have the right to reverse his pardon. A governing party spokesman, Rafal Bochenek, slammed the court ruling as “anti-democratic.”

Opposition politicians who accuse government authorities of eroding democratic norms welcomed the court’s ruling in the case of Mariusz Kaminski, who is currently the interior minister and head of the secret services.

It was the latest blow to Law and Justice in recent days after frustration with the government boiled over into a mass protest of hundreds of thousands of people on the streets of Warsaw and other cities on Sunday. The next day, the European Union’s top court dealt another setback to the government in Warsaw, ruling that a crucial part of Poland’s judicial overhaul infringes EU law.

Kaminski was sentenced in 2015 by a court to three years in prison for abuse of power as head of a state anti-corruption office, the Central Anticorruption Bureau, a job he held from 2007-2009. His former deputy, Maciej Wasik, also now his deputy interior minister, and two other anti-corruption bureau officials, were convicted.

They appealed their convictions, but Duda pardoned them in 2015 before the appeals court heard their case.

His pardon came a day after Kaminski was named minister for secret services in a new government formed after Law and Justice won the 2015 election. Kaminski is an ally of the powerful party leader, Jaroslaw Kaczynski.

The pardon was controversial at the time, with many legal experts arguing that presidential pardons are to be reserved for cases that have gone through all appeals.

“Wasik and Kaminski should be expelled from the government,” leftist lawmaker Krzysztof Gawkowski said. “There is no place in the government for people who have convictions and unfinished proceedings on their account.”

The Supreme Court said in its oral justification on Tuesday that Duda had no power to issue his pardons.

“The administration of justice in the Polish legal order is the exclusive domain of the common courts and the Supreme Court,” Judge Piotr Mirek said.

But the matter is even more complicated.

The legal overhaul that Law and Justice began after winning the 2015 election has also led to chaos in the judiciary, with critics of the government changes often speaking of “pseudo rulings” by “pseudo courts” that have been taken over in a process that is illegal under Polish law.

The government has managed to impose political control over another top court, the Constitutional Tribunal, which ruled last week that the Supreme Court didn’t have the right to exercise legal control over presidential pardons.

The Supreme Court judge, Mirek, disputed that, saying: “The Supreme Court is of the opinion that the judgment of the Constitutional Tribunal … did not have any legal consequences.”