KAMPALA, Uganda (AP) — A Ugandan academic who was imprisoned after insulting the president was freed Thursday by a judge who said she had been wrongfully convicted.

Frank Baine, a spokesman for the prisons service, said Stella Nyanzi was driven back to the maximum-security prison to collect her belongings after the high court ordered her release.

Chaotic scenes erupted outside the courtroom as her supporters tried to protect her from what they believed was an attempt to re-arrest her. Authorities in the East African nation sometimes re-arrest freed suspects by asserting fresh charges.

Nyanzi, who in a Facebook post once described Uganda’s longtime President Yoweri Museveni as “a pair of buttocks,” was sentenced to 18 months in August after being found guilty of cyber harassment.

The former academic fellow at Makerere University attracted the attention of authorities with bold, often profane descriptions of alleged shortcomings of the Ugandan government, including for its failure to honor a pledge to provide free sanitary pads to schoolgirls.

Amnesty International and several Ugandan rights groups had called for Nyanzi’s unconditional release. She has a loyal following among some Ugandans who believe she has been unfairly treated for openly criticizing the government.