The U.S. government lost a legal battle over its attempt to deport children with special humanitarian status who are waiting for their green cards.

In an unprecedented move, the federal government had sought to deport kids who were well on their way to getting legal permanent residency in the United States. But a new federal appeals court order, which notes that the children are a “hair’s breadth” from getting legal status, prevents U.S. Immigration Customs and Enforcement from sending the children to their home countries without due process.

“It’s important that the court recognized that these children do have constitutional rights, and they have the ability to go to court and seek review,” said Jacquelyn Kline, one of the children’s attorneys.

The decision emerges from a federal class-action lawsuit filed in Pennsylvania by four children who fled to the United States with their mothers to evade violence and gangs in Central America. They had asked the courts to halt their deportation and release them from detention since they had already been granted special humanitarian status.

We’ve been telling stories that change laws and lives for more than 40 years. And we’re just getting started. Sign up for our newsletter.

One of the four children who brought the case is a child known only as V.G. in court documents. (The courts protect the names of children in cases such as this.) He left rural El Salvador in 2015 when he was 14 following aggressive gang recruitment. He and his mother were caught swimming across the Rio Grande and they were arrested and put into expedited removal, a deportation process that limits immigrant access to the American legal system and gives border officers nearly unchecked authority to send away migrants they encounter.

The government’s unusual effort to deport V.G. years later — even though he’s close to getting a green card — stems from that expedited removal.

And the ensuing legal disputes over his deportation offer a view into how the tangle of America’s immigration policies sometimes operate in conflict with one another. In V.G.’s case, the federal government was trying to deport him, even though both the U.S. Citizenship and Immigration Service and a state court judge had determined it would not be in his best interest to send him back to his home country.

Normally, expedited removal is a speedy process, and there are few ways to contest it. But V.G. and his mother asked for asylum, which slows things down. They were denied asylum but while they pushed their case, they were detained in the Berks Family Residential Center in southeast Pennsylvania for about two years.

In the fall of 2016, V.G. went before a state court judge to gain Special Immigrant Juvenile Status, which Congress created to protect vulnerable children from deportation. He then applied for a green card and by early 2017, he had received a work authorization card, though he wasn’t able to immediately use it since he was still in detention.

Given the current two-year wait for a green card for many applicants from Central America, V.G. had to wait for his name to move up the list before he could adjust his status. In the meantime, V.G. asked the government to put a hold on their deportation cases but the government refused.

Instead, immigration officials took the position that despite his special status and green card prospects, it had the authority to send V.G. back home because of expedited removal. It argued that the courts have repeatedly found that immigrants in expedited removal aren’t entitled to due process or rights under the U.S. constitution.

Indeed, in most expedited removal cases, whatever the government says goes.

But the new court decision points out that V.G. and the other children who brought the case are in a different situation. It held that immigrants who have a substantive tie to the United States — such as minors like V.G. who have jumped through various legal and procedural hoops to get Special Immigrant Juvenile Status — have enough of a connection to the country to give them constitutional rights and access to American courts.

As a result, the case could have significant impact for other children with the same status. “The ruling is good for Special Immigrant Juvenile Status beneficiaries in general because it’s saying, We have these regulations for a specific reason, which is to protect these vulnerable children, and they have rights and benefits given to them and immigration authorities have to respect that,” Kline said.

It also could create an opening for other immigrants with with strong ties to the country to argue that they should be able to fight their expedited removal orders. “What this order is saying is that in limited circumstances, there could be some federal court review of expedited removal,” says Jennifer Koh, a law professor at Western State College of the Law and the director of its immigration legal clinic.

The U.S. Department of Justice, which litigated this case, did not respond to a request for comment.

As for V.G., he was released on bond last summer. Now he lives in New York with his mom. He goes to high school and is now making use of that work authorization card at a part-time job.

But he’s still waiting for his green card. And his legal case isn’t over yet either. The recent court decision, while a positive development for him, sent the case back to a lower court so that his case can eventually be resolved in a way that’s consistent with the new order.

V.G.’s case also raised various concerns about how immigration detention affects children’s emotional and social development. In a mental health evaluation conducted as part of this case, a clinical social worker observed that V.G. was showing signs of depression, hopelessness and anxiety. His mental health has “clearly suffered from living in the facility,” the report notes, and “further detention will cause additional undue stress.”

V.G.’s attorneys say that this case also serves as a cautionary tale about the concerns that come with the long-term detention of children in immigration lockup — which the government now seeks through its most recent executive order to end family separation at the border.

CarolAnne Donohoe, another attorney representing V.G. said that “any detention, even with their parent, is traumatic, particularly given that these children have already been traumatized — that’s why they fled.”

Everyone should have access to the facts. Yes, I want to help!