A federal appeals court in San Francisco has ruled President Donald Trump’s administration wrongly transferred more than $2 billion from military construction projects to build sections of the U.S. border wall with Mexico.

The 9th U.S. Circuit Court of Appeals ruled the administration illegally sidestepped Congress, which gets to decide how to use the funds. The court agreed with a coalition of border states and environmental groups that contended the money transfer was unlawful and that building the wall would pose environmental threats.

The latest twist in the legal battle that has largely gone Trump’s way came after the Supreme Court previously allowed the $2.5 billion to be spent while the litigation continued, blunting the impact of the latest appeals court action.

The administration has already awarded much of the money, including a $1.3 billion job in Arizona that was announced last month. Trump visited Yuma, Arizona, this week to mark completion of the 200th mile of the border wall during his administration, much of it with the transferred military funds that the 9th Circuit panel found illegal.

After the $2.5 billion transfer of military funds, the Pentagon diverted another $3.6 billion that an appeals court in New Orleans ruled in January could be spent.

The panel ruled the Trump administration not only lacked the authority to authorize the transfer of funds “but also violated an express constitutional prohibition designed to protect individual liberties.”

The vote was 2-1, with judges appointed by former President Bill Clinton in the majority and a Trump nominee dissenting.

The panel also held that the government failed to show the border wall construction would halt the flow of illegal drugs. It said the administration had cited drug statistics but didn’t address how.

“The executive branch’s failure to show, in concrete terms, that the public’s interest favors a border wall is particularly significant given that Congress determined fencing to be a lower budgetary priority and the Department of Justice’s data points to a contrary conclusion,” the majority wrote.

California Attorney General Xavier Becerra, who led a 20-state coalition of attorneys general which sued the administration, praised the decision.

“Today, the court reminded the president — once again — that no one is above the law,” he said in a statement. “While the Trump administration steals public funds to build an unauthorized wall at the southern border, families across the country are struggling to pay their bills. They deserve to know that their hard-earned dollars are going where Congress intended — to benefit them and their communities.”

The Associated Press contributed to this story.