SAN FRANCISCO (AP) — A federal judge blocked on Friday President Donald Trump from building sections of his long-sought border wall with money secured under his declaration of a national emergency.

U.S. District Judge Haywood Gilliam, Jr., on Friday immediately halted the administration’s efforts to redirect military-designated funds for wall construction. His order applies to two high-priority projects to replace 51 miles (82 kilometers) of fence in two areas on the Mexican border.

Gilliam issued the ruling after hearing arguments last week in two cases. California and 19 other states brought one lawsuit; the Sierra Club and a coalition of communities along the border brought the other. His ruling was the first of several lawsuits against Trump’s controversial decision to bypass the normal appropriations process to pay for his long-sought wall.

Gilliam, an appointee of President Barack Obama, said the plaintiffs were likely to succeed on arguments that the president was wrongly ignoring Congress’ wishes.

“Congress’s ‘absolute’ control over federal expenditures_even when that control may frustrate the desires of the Executive Branch regarding initiatives it views as important_is not a bug in our constitutional system. It is a feature of that system, and an essential one,” he wrote in his 56-page opinion.

A judge in Washington, D.C., is hearing a similar challenge brought by the U.S. House of Representatives that argued the money shifting violates the constitution. The judge was weighing whether the lawmakers even had the ability to sue the president instead of working through political routes to resolve the bitter dispute.

At stake is billions of dollars that would allow Trump to make progress in a signature campaign promise heading into his campaign for a second term.

Trump declared a national emergency in February after losing a fight with the Democratic-led House over fully paying for the wall that led to a 35-day government shutdown. As a compromise on border and immigration enforcement, Congress set aside $1.375 billion to extend or replace existing barriers in Texas’ Rio Grande Valley, the busiest corridor for illegal crossings.

Trump grudgingly accepted the money, but then declared the national emergency to siphon money from other government accounts because he wanted to spend $8 billion on wall construction. The funds include $3.6 billion from military construction funds, $2.5 billion from Defense Department counterdrug activities and $600 million from the Treasury Department’s asset forfeiture fund.

The president’s adversaries say the emergency declaration was an illegal attempt to ignore Congress, which authorized far less wall spending than Trump wanted.

“We welcome the court’s decision to block Trump’s attempts to sidestep Congress to build deadly walls that would hurt communities living at the border, endanger wildlife, and have damaging impacts on the environment,” said Andrea Guerrero, a member of the Southern Border Communities Coalition.

The administration said Trump was protecting national security as unprecedented numbers of Central American asylum-seeking families arrive at the U.S. border.

It wasn’t a total defeat for the administration.

Gilliam rejected a request by the 20 states to block use of Treasury asset forfeiture funds for border wall construction. The states argued that Trump skirted environmental impact reviews but the judge said they were unlikely to prevail on that point.

The administration has said it plans to use the Treasury money to extend barriers in the Rio Grande Valley.

The courtroom showdowns come amid a flurry of activity to accelerate wall construction.

The preliminary injunction applies to the two highest-priority Pentagon-funded wall contracts.

The Defense Department has transferred $2.5 billion to border wall coffers.

The Defense Department transferred $1 billion to border wall coffers in March and another $1.5 billion earlier this month. Patrick Shanahan, the acting defense secretary, is expected to decide soon whether to transfer an additional $3.6 billion.

The Army Corps of Engineers recently announced several large contacts with Pentagon funding. Last month, SLSCO Ltd. of Galveston, Texas, won a $789 million award to replace 46 miles (74 kilometers) of barrier in New Mexico.

Last week, Southwest Valley Constructors of Albuquerque, New Mexico, won a $646 million award to replace 63 miles (101 kilometers) in the Border Patrol’s Tucson, Arizona, sector. Barnard Construction Co. of Bozeman, Montana, won a $141.8 million contract to replace 5 miles (8 kilometers) in Yuma and 15 miles (24 kilometers) in El Centro, California.

Aside from California, states participating in the legal challenge are Colorado, Connecticut, Delaware, Hawaii, Illinois, Maine, Maryland, Massachusetts, Michigan, Minnesota, Nevada, New Jersey, New Mexico, New York, Oregon, Rhode Island, Vermont, Virginia and Wisconsin.