A federal judge in New York has ruled that acting homeland security secretary Chad Wolf lacked the authority to limit the work permits of hundreds of thousands of undocumented immigrants who came to the United States as children because his appointment to the top position in the department did not appear to be lawful.

U.S. District Judge Nicholas G. Garaufis — one of the first judges to block President Trump from phasing out the work permits the government issues via the Deferred Action for Childhood Arrivals program, or DACA — said in an unusual Saturday ruling that the “plain text” of the department’s order of succession showed that Wolf’s ascension to acting secretary did not follow established law and was part of several hastily crafted administration moves designed to get people into the top DHS position outside of standard procedures.

As a result, Garaufis said, Wolf’s memo in July limiting the validity of the DACA permits from two years to one year “was not an exercise of legal authority.”

Garaufis’s ruling adds to the complex legal battles surrounding the Obama-era DACA program, which was designed to give those who arrived in the United States as minors a way to stay in the country. More than 640,000 immigrants known as “dreamers” currently rely on the program to live and work in the United States.

The practical effect of the judge’s decision was not immediately clear because he did not explicitly order DHS to restore the original version of DACA and has asked both sides to schedule a conference on the matter.

Several related lawsuits also are pending, setting up a potential conflict among courts. One key lawsuit, in Texas, is considering the legality of the program, and a hearing in that case is scheduled for December.

President-elect Joe Biden, who takes office in January and was vice president when President Barack Obama created DACA, has pledged to restore the original program.

The Department of Homeland Security has argued that Wolf was serving lawfully when he issued the DACA memo in July, according to the court.

DHS criticized Garaufis’s ruling Sunday as “another example of an activist judge substituting his own policy preference for those of the Trump Administration” and accused him of having an “unwillingness to seriously engage on the facts or the law.”

“DHS is exploring its options to ensure its review of DACA continues as intended,” the spokesperson, who was not identified, said in an email.

Immigrant advocates cheered the ruling and said they hoped it would lead to a faster restoration of the original program.

Karen Tumlin, a lawyer for the immigrants in the case, said the judge also certified a class of about 1.1 million immigrants who could be affected by the program, many of whom are first-time applicants who were still in high school when courts froze DACA.

“There’s an urgency; people have been waiting since 2017,” said Tumlin, director of the Justice Action Center. “It is unacceptable for the Trump administration to cut corners and make changes in unlawful ways. I consider it a big victory.”

New York Attorney General Letitia James, who also urged the court to declare the memo unlawful, said the courts have been continuously ruling in favor of DACA recipients.

“The Supreme Court already ruled that President Trump’s attempts to unfairly punish dreamers was unlawful, but now another federal court has ruled that this administration’s attempts to circumvent the courts in an effort to target and punish innocent young people is just as unlawful,” James said.

The U.S. Supreme Court blocked Trump from ending the Obama-era program in June, saying the government had failed to provide the required legal justification for the move, and justices ordered DHS to consider next steps. Immigrants had hoped DHS would restore the original program and reopen it to new applicants who had not been allowed to apply since 2017.

Instead, Wolf issued the July memo shortening the validity of the work permits. Wolf has said that DACA “presents serious policy concerns” that might warrant its termination, but he has not made a final decision about the program’s future.

The memo prompted lawyers to ask Garaufis to intervene. They argued that Wolf’s appointment was unlawful, and, by extension, so was his July memo.

The Government Accountability Office said in August that Wolf and his acting deputy, Ken Cuccinelli, were serving unlawfully in their roles, a knock against Trump’s penchant for installing “acting” leaders who lack Senate confirmation, partly because it is easier to fire them.

DHS has called the watchdog agency’s findings “baseless.”

Trump has said he prefers people in acting roles because “it gives you great, great flexibility” to remove them.

The judge’s ruling Saturday lays out a complex series of administrative moves that appear aimed at allowing the Trump administration to sit its desired officials in the top DHS role despite legal hurdles. He wrote that authorities reworked the department’s order of succession several times to leapfrog candidates over those who — under normal circumstances — would take over the role should a DHS secretary be fired, resign, face incapacitation or die in office.

The judge found that several DHS leaders reordered the succession to allow people not in the normal chain to take over the department, sometimes on the departing acting secretary’s last day.

Wolf is Trump’s fifth homeland security secretary, an unusually high number for the agency, and he was sworn in as acting secretary in November 2019. Trump formally sent his nomination to the Senate in September.

In a letter Friday, the Justice Department notified Garaufis of an “inadvertent factual inaccuracy” regarding the ratification of the Wolf memorandum and acknowledged confusion about the timeline leading to Wolf’s nomination in September.

The Justice Department said in the letter that DHS had late Thursday night notified the agency that Pete Gaynor, chief of the Federal Emergency Management Agency, had briefly assumed control of DHS to issue a succession order so Wolf could take over, in case Wolf had not been lawfully appointed.

Justice Department lawyers said Gaynor’s order might have been issued as little as an hour before Wolf’s nomination arrived at the Senate, and not afterward as government lawyers had told the court. Justice Department lawyers said they did “not yet know the precise timing of events” and were trying to find out.

But the judge said that did not matter. He said Gaynor could not fleetingly serve as secretary just for the “sham purpose” of securing Wolf’s foothold on the job.

“The court wishes the government well in trying to find its way out of this self-made thicket,” the judge wrote.