MARGATE — The federal government has agreed to clean up the mess it created on a New Jersey beach, where flooding from a dune construction project forced beachgoers to wade through vile water or take blocks-long detours to reach the ocean.

Jordan Rand, the lawyer for Margate, told The Associated Press that the town, state and federal governments filed an agreement with U.S. District Court on Tuesday night.

It calls for construction of an underground pipe system to prevent water from collecting between the dunes and the town's wooden bulkhead.

The plan still must be signed by a federal judge.

"This should eliminate the problem," said Rand, who unsuccessfully sued the federal government on behalf of homeowners and then the town to try to block the dune construction project. The lawsuits predicted in minute detail almost exactly the type of flooding Margate feared would occur if the dunes were built.

The dunes are part of New Jersey Gov. Chris Christie's plan for a nearly unbroken line of storm defense along the state's 127-mile coastline, something he insisted is necessary following the catastrophic damage Superstorm Sandy wreaked on parts of the shore in 2012.

Margate, an upscale shore town south of Atlantic City, argues that its wooden bulkhead provides adequate protection against ocean storm surge, and claims most of its Sandy damage came from bay flooding on the other side of town.

The solution agreed upon Tuesday night will involve the U.S. Army Corps of Engineers installing pipes underground, running parallel to the beach to collect water that otherwise would have ponded on the beach. Every few blocks, outfall pipes will carry that water back out to the ocean, Rand said.

The federal government will pay the entire cost of the project, which has not yet been calculated, he said.

Representatives of the Army Corps did not immediately respond to requests for comment Wednesday. A consent decree filed Tuesday night shows both sides in agreement with the plan, including that it "will be paid for with federal funds allocated for the federal project."

Last month, New Jersey Superior Court Judge Julio Mendez expressed shock at photos showing "lake-like" ponds of standing water on the beach in Margate for days following heavy rains, and temporarily shut down the project. But a federal court allowed it to resume while both sides worked toward a permanent solution.

When storms dumped nearly a half foot of rain on Margate, water quickly collected in excavated areas between the dunes and the wooden bulkhead that separates oceanfront homes from the sand. Some were 2 or more feet deep, and lay stagnate until the Army Corps began pumping them out days later.

Mayor Michael Becker said the city's testing of water from the ponds found "off the charts" levels of bacterial contamination. He said two lifeguards contracted bacterial infections by sloshing through the dirty water and were out sick from work.

Copyright 2017 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.

More From New Jersey 101.5

More From New Jersey 101.5 FM