Guatemala’s Otto Perez resigned as president and was jailed on Thursday while a judge weighs charging him in a corruption scandal that gutted his government and plunged the country into a political crisis days before a presidential election.

In an emergency session, Congress approved Perez’s resignation after the retired general quit overnight. Former vice president Alejandro Maldonado was sworn in as president to fill out the remaining months of Perez’s term.

Tens of thousands of protesters had flooded the streets of the capital and other cities in recent weeks, calling for Perez to step down over allegations he was involved in a customs racket.

Celebrations over Perez’s resignation erupted in a plaza of the capital on Thursday, as the country prepared for presidential and congressional elections on Sunday.

“I am going to respect due process and face this,” Perez told reporters after a court hearing and before he was escorted away to jail because he was deemed a flight risk. “I believe this is completely inconsistent.”

Perez, 64, has repeatedly denied any wrongdoing. The court hearing is set to continue on Friday.

In his resignation letter, submitted to Congress during the night, Perez said he would face the charges against him “with a clear conscience.”

Prosecutors allege he was involved in a customs scam dubbed “La Linea” (“The Line”), referring to a phone hotline used by importers to avoid paying customs duties in exchange for bribes.

“At last, the most corrupt president in the history of Guatemala is gone,” said Juan Carlos Carrera, a 38-year-old lawyer who waved a blue and white Guatemalan flag from his car window and honked the horn.

“This is a citizens’ revolution, because Guatemala has been divided for many years with so much injustice,” he said.

Dressed in a dark suit, Perez sat in the courtroom while prosecutors played phone recordings that allegedly implicate him in the scandal.

Prosecutors have said the charges to be brought against Perez are illicit association, taking bribes and customs fraud, but he has not yet been charged.

The White House said it respected Perez’s decision to resign, and added that it was ready to work with Maldonado in his new role as president.

“We commend the people of Guatemala and their institutions for the manner in which they have dealt with this crisis, and continue to underscore our support for Guatemala’s democratic and constitutional institutions,” White House spokesman Josh Earnest told reporters.

Perez was elected in late 2011 after promising to fight crime and corruption. Under Guatemala’s constitution, he was not allowed to seek re-election in Sunday’s presidential vote.

He had resisted calls for his resignation but his options narrowed on Wednesday when lawmakers stripped him of immunity from prosecution and a judge issued a detention order against him.

Attorney General Thelma Albania said Perez was also being investigated for money laundering, which could lead to the freezing of his assets.

His conservative government spent much of this year facing corruption allegations, and he fired several of his cabinet members in a purge in May.

Former Vice President Roxana Baldetti resigned after she was linked to “La Linea.” She denied any wrongdoing but was arrested on the same charges Perez now faces.

More than 20 other officials, including the president of the central bank, have been arrested over the scam, though how much money was involved is still unclear.

Prosecutors and a powerful U.N.-backed anti-corruption body known as the CICIG moved against Perez following months of investigations and findings taken from some 89,000 telephone taps, almost 6,000 emails and 17 raids.

The anti-corruption investigations have also hit the center-right opposition Lider party, whose election candidate, Manuel Baldizon, had been leading the polls.