The risk of Tunisians electing an imprisoned candidate were averted on Wednesday when media mogul Nabil Karoui was released from prison, days before Sunday’s election runoff after weeks of detention waiting for a verdict in his corruption trial.

Karoui was the runner up in the first round of the presidential election last month despite being held since August 23 in pre-trial detention. He will now face Kais Saied, an independent, in the second round of voting.

Hundreds of supporters who had gathered outside the prison cheered as Karoui departed, a Reuters journalist said, hours after an appeal court ruled that he should be released.

Karoui still faces charges of money laundering and tax fraud, which he denies, but no date has been publicly set to hand down a final verdict in the case. He remains subject to a travel ban and asset freeze.

The release means a televised debate can now take place between Saied and Karoui, who missed Tunisia’s first ever such event between the 26 candidates who ran in the initial round of the election.

His release could also make it easier for his party, Heart of Tunisia, to negotiate with the moderate Islamist Ennahda party to form a coalition government, though both have publicly resisted working together.

Exit polls project Ennahda taking first place and Heart of Tunisia second in last Sunday’s separate parliamentary election, for which official results will be announced later on Wednesday.

Last week, interim president Mohamed Ennaceur said Karoui’s detention and inability to campaign had damaged the credibility of the election.

Election watchdogs had also called for his release saying there could be no fair vote if he was detained.

“His release saved our transition and the situation at the last moment … We were in a very difficult moment in Tunisia which really threatened Tunisian democracy,” Karoui’s spokesman Hatem Mliki said.

Tunisia’s electoral commission has itself warned that Karoui could appeal against the result if he loses as he has been denied equal opportunity to communicate with voters, and that the result could be annulled.

Karoui took 15.6% of the vote in the first round of the election three weeks ago, while retired law professor Saied came first with 18.4%.

The case against Karoui was brought three years ago by I Watch, the local chapter of Transparency International, an anti-corruption watchdog.

Critics regard Karoui as a corrupt populist wrongfully manipulating his television station and a charity he runs for personal political gain, but his supporters see him as a champion of Tunisia’s poor.