The U.S. railway whose runaway train killed 47 people in a tiny Quebec town last month was granted bankruptcy protection from a Canadian court on Thursday and took steps in that direction in a U.S. court as well.

Montreal Maine & Atlantic railroad filed for protection in both countries on Wednesday, saying its revenues had deteriorated since the July 6 crash and it could not afford to pay its mushrooming financial obligations.

The company’s runaway crude oil train derailed in the small lakeside town of Lac-Megantic, Quebec, exploding in huge fireballs that destroyed a swathe of the town’s core. About 5.6 million liters of crude oil were spilled, and MMA estimated the cleanup costs would exceed C$200 million.

Quebec Superior Court Judge Martin Castonguay called the company’s behavior “deplorable” and said he was not impressed by its management.

“This decision is to prevent legal anarchy,” Castonguay told the courtroom after approving the bankruptcy protection for MMA’s Canadian unit.

Also on Thursday, a U.S. federal judge in Maine ordered the appointment of a federal trustee to oversee MMA’s bankruptcy proceedings and help ensure that the railroad remains in operation so that service continues for local companies.

Thursday’s decision allows the railway to continue to operate and meet its payroll obligations through Aug. 22, when another hearing will be held.

The judge in Maine questioned whether the company could remain afloat without its approximately $1 million a month crude oil transport business.

Despite MMA’s decision to stop shipping crude after the crash, company lawyer Roger Clement assured Judge Louis Kornreich that the business would remain viable.

“The railroad had a very healthy business before the hauling of crude oil started about 18 months ago. We’re very hopeful that it can bring its revenues back up,” he said.

After the hearing, Clement told reporters that he thought the sale of MMA’s assets to another company interested in shipping crude between Montreal and Saint John, New Brunswick – the line’s eastern terminus and home to Irving Oil’s refinery – was a “distinct possibility.”

“I expect there will be a lot of interest from other rail operators in purchasing the lines of Montreal, Maine and Atlantic. There’s no other route that’s as direct,” he said.

The bankruptcy filing sparked anger in Lac-Megantic, where residents fear victims’ families may not get the compensation they are seeking through class-action and individual suits against the company in U.S. and Canadian courts.

The governments of Quebec and the town of Lac-Megantic have demanded MMA foot the cleanup bill, which already amounts to C$7.8 million ($7.6 million).

Quebec Health Minister Rejean Hebert said the provincial government was seeking status in the bankruptcy case as a secured creditor, which would assure it would receive payment from MMA before some other claimants.

The U.S. government and Canada’s federal and provincial government are the company’s biggest secured or potential secured creditors.

Hebert said the Quebec government would take priority over the U.S. government on claims against MMA’s Canadian unit.

MMA’s Canadian petition said insurance covered liabilities up to C$25 million, far too little to cover damages.