In a setback for the Trump administration, a federal judge in Montana temporarily halted construction of the Keystone XL oil pipeline late on Thursday on the grounds that the U.S. government did not complete a full analysis of the environmental impact of the TransCanada Corp project and failed to justify its decision granting a permit for the 1,200-mile long project designed to connect Canada’s tar sands crude oil with refineries on the Texas Gulf Coast. The ruling came in a lawsuit that several environmental groups filed against the U.S. government in 2017, soon after President Donald Trump announced a presidential permit for the project.

The judge, Brian Morris of the U.S. District Court in Montana, said President Trump’s State Department ignored crucial issues of climate change in order to further the president’s goal of letting the pipeline be built. In doing so, the administration ran afoul of the Administrative Procedure Act, which requires “reasoned” explanations for government decisions, particularly when they represent reversals of well-studied actions.

Morris wrote that a U.S. State Department environmental analysis “fell short of a ‘hard look’” at the cumulative effects of greenhouse gas emissions and the impact on Native American land resources. He also ruled the analysis failed to fully review the effects of the current oil price on the pipeline’s viability and did not fully model potential oil spills and offer mitigations measures.

However, the decision does not permanently block a pipeline permit. It requires the administration to conduct a more thorough review of potential adverse impacts related to climate change, cultural resources and endangered species. The court essentially ordered a do-over.

Morris, a former clerk to the late Chief Justice William Rehnquist, was appointed to the bench by President Obama.

* * *

The ruling is a victory for environmentalists, tribal groups and ranchers who have spent more than a decade fighting against construction of the pipeline that will carry heavy crude to Steele City, Nebraska, from Canada’s oilsands in Alberta.

“The Trump administration tried to force this dirty pipeline project on the American people, but they can’t ignore the threats it would pose to our clean water, our climate, and our communities,” said the Sierra Club, one of the environmental groups involved in the lawsuit, adding that “today’s ruling makes it clear once and for all that it’s time for TransCanada to give up on their Keystone XL pipe dream." The lawsuit prompting Thursday’s order was brought by a collection of opponents, including the indigenous Environmental Network and the Northern Plains Resource Council, a conservation coalition based in Montana.

On the other hand, the ruling was a major defeat for Trump, who attacked the Obama administration for stopping the project in the face of protests and an environmental impact study. Trump signed an executive order two days into his presidency setting in motion a course reversal on the Keystone XL pipeline as well as the Dakota Access pipeline.

In addition to the president, the ruling deals a major setback for TransCanada and could possibly delay the construction of the $8 billion, 1,180 mile (1,900 km) pipeline. It’s intended to be an extension of TransCanada’s existing Keystone pipeline, which was completed in 2013. Keystone XL (the initials stand for “export limited”) would transport up to 830,000 barrels of crude oil per day from Alberta, Canada, and Montana to Oklahoma and the Gulf Coast. In the U.S., the pipeline would stretch 875 miles through Montana, South Dakota and Nebraska, with the rest continuing into Canada.

Trump supported building the pipeline, which was rejected by former President Barack Obama in 2015 on environmental concerns relating to emissions that cause climate change. Trump said the project would lower consumer fuel prices, create jobs and reduce U.S. dependence on foreign oil.