FLINT, Mich. -- Michigan and the city of Flint have agreed to replace thousands of home water lines under a sweeping deal to settle a lawsuit over lead-contaminated water in the troubled city.

A court filing Monday says Flint will replace at least 18,000 lead or galvanized-steel water lines by 2020, and the state will pick up the bill with state and federal money. The state said it will pay $87 million and keep another $10 million in reserve if necessary.

The settlement will be presented to a federal judge in Detroit on Tuesday for approval.

Flint’s water was tainted with lead for at least 18 months, as the city tapped the Flint River but didn’t treat the water to reduce corrosion. As a result, lead leached from old pipes and fixtures.

Get Breaking News Delivered to Your Inbox

Residents’ mistrust in government has remained high nearly three years after a fateful switch of the city’s water source in April 2014. Flint’s public health emergency began when officials failed to properly treat lead lines for corrosion.

The state acknowledged the lead problem in October 2015. Lead from old pipes leached into the water supply because corrosion-reducing phosphates were not added due to an incorrect reading of federal regulations.

Elevated levels of lead, a neurotoxin, were detected in children, and 12 people died in a Legionnaires’ disease outbreak that experts suspect was linked to the improperly treated water.