Bankrupt oil companies must pay the costs of cleaning up their abandoned wells, Canada’s top court ruled, striking down a decision that allowed them to pass those costs onto an industry-funded group.

The Supreme Court of Canada, in a 5-2 decision, said companies or their receivers can’t walk away from their environmental liabilities by invoking federal legislation regarding the order in which creditors are to be paid.

Thursday’s ruling in the case of Redwater Energy Corp., which filed for bankruptcy in late 2015, shifts the cost of remediating spent or inactive wells onto the buyers of bankrupt companies instead of allowing them to pass those expenses onto the industry-funded Orphan Well Association. The ruling comes amid tough times for the energy industry that have increased the backlog of dead wells.

Funded primarily through a levy charged to oil producers, the Orphan Well Association received a $235 million loan from Alberta’s provincial government in 2017 to help it deal with its rising backlog. The group currently lists more than 3,100 orphan wells for abandonment in its inventory.

The receiver that was liquidating Redwater had argued it should be able to sell the company’s best wells and leave the worst behind for the Orphan Well Association to clean up, otherwise loans to energy companies may become too risky. Alberta regulators countered that buyers should have to take both good and bad wells, balancing the risks to creditors and taxpayers.

“The decision will likely lower the future burden on Alberta’s Orphan Well Fund, which is funded by all solvent producers,” RBC Capital Markets analyst Keith Mackey said in a note to investors. “The ruling will also heighten overall scrutiny of abandonment liabilities by lenders and buyers of mature assets.”