A Miami federal judge ordered Immigration and Customs Enforcement to halt its planned deportation of 92 Somalis after what was described as a harrowing two-day ordeal on an initial attempt.

“The judge acted just in time,” Rebecca Sharpless, lead attorney on the lawsuit and director of the Immigration Clinic at the University of Miami School of Law, said Wednesday. ”The government confirmed that our clients would have been on a flight to Somalia” Wednesday morning if not for the order issued late Tuesday following a hearing by phone.

A class action lawsuit claimed the Somalis were bound and shackled on an airplane for nearly two days, some urinating on themselves, during a botched U.S. deportation attempt that began Dec. 7.

U.S. immigration agents “kicked, struck, choked and dragged detainees” during the journey, and some of the Somalis were put in straitjackets.

U.S. District Judge Darrin Gayles issued a three-page order setting a hearing Jan. 2 to review jurisdictional issues and said his order would remain in effect until a minute before midnight that day.

Attorneys for the Somalis claimed “immigration circumstances have changed based on the U.S. government's failed attempt to repatriate them to Somalia and the resulting international news attention, which now makes their return to Somalia unsafe,” Gayles wrote in his order.

Based on the special circumstances, he said, “A short stay of removal is warranted.”

The judge also ordered access to “adequate medical treatment” and “reasonable access” to attorneys.

Immigration advocates maintain one of the biggest obstacles for people seeking asylum in the United States is lack of immediate access to attorneys.

The initial flight to Somalia from Louisiana reached Dakar, Senegal, before sitting on the runway for 23 hours and returning to the U.S. because the relief flight crew did not have enough rest to fly, the complaint said.

Lawyers are alarmed that the U.S. is returning a growing number of people to a country that has long been a war zone. “It is not safe for these men and women to return, especially in light of the escalation of terrorist violence in Somalia in the last weeks,” Sharpless said.

The Somalia-based al-Shabab extremist group was blamed for a massive truck bombing in the capital, Mogadishu, that killed 512 in October. Only a few attacks since 9/11 have killed more people. Al-Shabab, the deadliest Islamic extremist group in Africa and the target of more than 30 U.S. airstrikes under the Trump administration, holds large parts of rural southern and central Somalia.

A year-end report by ICE said 521 Somalis were deported this year, up sharply from 198 last year. Somalia by far saw the largest number of citizens deported home among African nations.

“It was not until the very end of 2016 and into 2017 that ICE sought for the first time to detain and then remove Somalis in larger numbers using charter flights,” the lawsuit said.

ICE does not comment on pending lawsuits and denied mistreating the Somalis on the failed deportation flight. It has said more than 60 of the Somalis had criminal convictions.

The lawsuit said U.S. immigration law forbids sending people home to countries where they could face persecution or torture, and al-Shabab is known to target U.S. deportees.

The Somalis, with family ties in Minnesota and elsewhere around the U.S., were held in two detention centers in South Florida.

There was no immediate comment from Somalia's government.

The lawsuit was filed in collaboration with Lisa Lehner of Americans for Immigrant Justice in Miami, Andrea Montavon-McKillip of Legal Aid Service of Broward County in Plantation and Benjamin Casper Sanchez of the James H. Binger Center for New Americans at the University of Minnesota Law School in Minneapolis.

The Associated Press contributed to this report.