Donald Trump’s executive order threatening to withhold funding from “sanctuary cities” that limit cooperation with immigration authorities is unconstitutional, but a judge went too far when he blocked its enforcement nationwide, a US appeals court ruled Wednesday.

In a 2-1 ruling, the ninth US circuit court of appeals agreed that the order exceeded the president’s authority.

Chief Judge Sidney Thomas wrote: “Absent congressional authorization, the administration may not redistribute or withhold properly appropriated funds in order to effectuate its own policy goals.”

But he also said there wasn’t enough evidence to support a nationwide ban on the order and sent the case back to the lower court for more hearings on that question.

US district judge William Orrick said in November that the order threatened all federal funding and that the president did not have the authority to attach new conditions to spending that was approved by Congress.

The ruling came in lawsuits filed by two California counties – San Francisco and Santa Clara. The executive order potentially jeopardized hundreds of millions of dollars in funding to the two counties, Orrick said, citing comments by Trump and US attorney general Jeff Sessions as evidence of the order’s scope.

The Trump administration said the order applied to a relatively small pot of money that already required compliance with immigration law.

Devin O’Malley, a spokesman for the US justice department, said the president’s order was legal. He called the court’s ruling a victory for “criminal aliens in California, who can continue to commit crimes knowing that the state’s leadership will protect them from federal immigration officers whose job it is to hold them accountable and remove them from the country”.

“The justice department remains committed to the rule of law, to protecting public safety and to keeping criminal aliens off the streets,” he said.

The executive order is part of a push by the Trump administration to crack down on cities and states that generally don’t comply with US immigration authorities. The administration has sued California over three laws aimed at protecting immigrants in the country illegally. It also has moved to block a key public safety grant from going to sanctuary cities and states.

The Trump administration says sanctuary jurisdictions allow dangerous criminals back on the street. San Francisco and other sanctuary cities say turning local police into immigration officers erodes the trust needed to get people to report crime.