A federal judge said Wednesday that BP must stand by the agreement it made with the companies it compensated for losses blamed on the 2010 Gulf oil spill.

BP argued that a flawed funding formula enabled nearly 800 businesses to overestimate their spill-related claims.

One construction company hundreds of miles from the coast received $13.2 million, but deserved $4.8 million at most, BP said. Another company selling "animals and animal skins" was overpaid about $14 million, and about 50 others shouldn't have been paid at all, the company said.

About 150 claimants should return a total of $185 million, and overpayments to the rest haven't been calculated, attorney Kevin Downey argued.

U.S. District Judge Carl Barbier was not persuaded, thwarting BP's latest attempt to control potential liabilities now approaching $50 billion.

The judge agreed weeks ago to change the compensation formula for any future payments, but ruled Wednesday that a deal is a deal when it comes to the money BP has already paid out. Under that deal, claimants agreed not to sue, and BP agreed that no future court action could change their payments.

"BP disagrees with today's decision and will appeal it," company spokesman Geoff Morrell said.

BP is still facing a financial nightmare from the nation's worst oil disaster, which began with an explosion that tore open the oil company's Macondo well on the floor of the Gulf of Mexico, destroying the Deepwater Horizon drilling rig and killing 11 workers.

Damage at the undersea wellhead sent millions of gallons of thick, raw oil spewing into the water, harming wildlife and the coast for months, until the U.S. Navy declared the well fully capped in Sept. 2010.

As the spill defied containment efforts, BP sought to restore its image and limit its liability by promising to compensate the victims.

Trying to force businesses to return that compensation could cost the London-based company some goodwill, but BP's financial pressures are different now.

"In 2010 and 2011, BP was willing to cut any deal necessary with anyone to reduce its legal risk," said Pavel Molchanov, an energy analyst for Raymond James. "Now the company is taking a more assertive approach."

BP's total potential liabilities now include up to $18 billion in fines and penalties that could be imposed for violating federal pollution laws, and more than $27 billion BP says it has already paid to restore the coast and settle damage claims.

The claims office said it has paid $4.1 billion to more than 50,700 people and businesses as of Wednesday, and it's not done yet — the settlement fund is not capped.

The Associated Press