PHOENIX (AP) — Conditions at most Border Patrol facilities in Arizona are punitive and unconstitutional, a U.S. judge in Arizona said Wednesday while ruling in favor of migrants who have long-complained about inhumane and unsanitary conditions in holding cells.

The ruling came weeks after the conclusion of a seven-day trial in which attorneys for migrants who sued in 2015 argued that the agency holds immigrants in extremely cold, overcrowded, unsanitary and inhumane cells.

The order makes permanent a preliminary injunction that U.S. District Court Judge David C. Bury issued in 2016 requiring the Tucson Sector to provide clean mats and thin blankets to migrants held for longer than 12 hours and to allow them to clean themselves.

It also bars the agency from holding migrants more than 48 hours if they’ve been fully processed, which is common when other agencies involved in taking the migrants, such as Immigration and Customs Enforcement, don’t have the capacity to pick them up in a reasonable amount of time.

Bury is also banning the use of bathrooms for sleeping, which came to light during the trial this year, when video was shown of a man trying to reach a bathroom but failing to because migrants were sleeping in them.

“Today’s decision is a tremendous victory for communities everywhere fighting courageously to uphold human dignity and the rights enshrined in our Constitution,” Alvaro M. Huerta, staff attorney at the National Immigration Law Center, said in a statement.

The center was one of the advocacy groups that brought the case forward. It was argued in court by attorneys for the law firm Morrison & Foerster.

“We are enthused that our justice system has intervened in a meaningful way to institute much needed change and hold CBP accountable,” Huerta said.

Customs and Border Protection didn’t immediately respond to a request for comment.

In his order, Bury said there’s no evidence CBP intends to create punitive conditions, but that the agency has stretched resources to “provide the best conditions of confinement available under the circumstances.”

Although the lawsuit predates last year’s surge in immigrant arrivals, it illustrates some of the challenges posed when migrants are detained, especially if they are children. Reports of hellish conditions circulated in many parts of the Texas border last year, where thousands of immigrants — largely families — crossed over from Mexico. In El Paso, an inspection by the government’s internal watchdog found there were 900 people crammed into a 125-person facility at one point in May.

READ MORE: Past decade has seen human rights ‘backlash,’ UN official says

In his order Wednesday, Bury wrote that the Border Patrol and its parent agencies, or the defendants in the case, “administer a detention system that deprives detainees, who are held in CBP stations, Tucson Sector, longer than 48 hours, of conditions of confinement that meet basic human needs.”

Conditions that migrants — who are considered civil detainees, not criminal — are subjected to after 12 hours are “presumptively punitive and violate the Constitution,” and are even worse than a criminal jail or prison, the judge wrote.

Bury has been critical of the agency, saying it has done little to remedy issues, especially around overcrowding and migrants’ inability to sleep.

“Nobody has done anything. Is that why a court has to jump in?” Bury asked during the last day of trial on Jan. 22. “It just seems like the lack of a response to these numbers just calls for a court order.”

Government attorneys said in their closing arguments last month that plaintiffs didn’t prove the agency violated any constitutional rights. It says many things are out of the agency’s control, such as whether other agencies involved in taking migrants have capacity.

Its facilities were built of short-term stays, for adults. Holding cells are in odd shapes, reducing the number of sleeping mats that can comfortably fit on the ground. On nights when agents arrest large groups of people, or when other agencies involved in immigration don’t have the capacity to pick them up, cells become extremely overcrowded.

A video displayed on the opening day of the trial showed a man walking over body after body as he tried to make his way to the bathroom. Once there, he realized all stalls had people sleeping in them.

Migrants have long decried conditions in Border Patrol facilities, now infamously known as hieleras, or iceboxes. And although the Tucson Sector hasn’t experienced the massive number of immigrants that other parts of the Southwest border has, the number of hours that migrants spend in custody there has continued to grow.

About 12,000 people were in custody for more than 72 hours in the Tucson Sector last year, or about 20%. The average time in custody was nearly 54 hours.

On Friday, another federal judge ruled in favor of immigrant s and against the government. The judge found the U.S. government in contempt after authorities deported five young immigrants who were seeking to remain in the country under a program for abused and neglected immigrant children.