This post has been updated.

Michigan and the city of Flint have agreed to spend the next several years replacing roughly 18,000 aging underground pipes as part of a far-reaching legal settlement over the city’s ongoing crisis involving lead-tainted water.

A settlement approved by a federal judge Tuesday will require the state to fund Flint’s efforts to replace the lead and galvanized water service lines by 2020. Under the agreement, the state will agree to pay $87 million for the undertaking and will keep another $10 million in reserve in case more pipes than expected need replacing. About $30 million of that money will come from the $100 million that Congress approved late last year in aid to Flint. The state will be responsible for the remainder.

“This is a win for the people of Flint,” Melissa Mays, a plaintiff in the case and one of the parents who first sounded the alarm about Flint’s water, said in a statement. “When the government fails to uphold democracy and protect our rights to clean water, we have to stand up and fight. The greatest lesson I’ve learned from Flint’s water crisis is that change only happens when you get up and make your voice heard.”

Dimple Chaudhary, a senior attorney with the Natural Resources Defense Council, which is among the environmental and civil rights groups behind the lawsuit, said the settlement will mean safer water for Flint residents.

“For the first time, there will be an enforceable commitment to get the lead pipes out of the ground. The people of Flint are owed at least this much,” she said.

Michigan Gov. Rick Snyder (R) said in a statement Tuesday that the settlement “continues the state’s commitment to providing the resources necessary for the residents of Flint to recover from the crisis.”

In addition to those “many different resources,” Snyder said, “the state will continue striving to work on many priorities to ensure the city of Flint has a positive future, including economic development, job placement and riverfront revitalization.”

Under the terms of the deal, state officials also must continue to deliver bottled water to housebound residents and must continue to operate free bottled water distribution centers around the city through early September, though it could begin to phase out some sites after May 1 if demand fades. The state also will continue to go door to door in the city through December 2018 to make sure people have properly installed water filters and to provide new filters and replacement cartridges.

The agreement comes barely a week after the Environmental Protection Agency formally awarded a $100 million grant to the state — money allocated by Congress — to fund upgrades to Flint’s outdated water system. While a portion of that money will go to ripping out old pipes that contributed to the city’s lead crisis, Flint also intends to spend a large portion of the grant money modernizing its water treatment plant, replacing meters and fixing distribution mains.

It has been nearly a year and a half since state officials acknowledged that Flint’s water was tainted with dangerous levels of lead, and more than two years since residents began complaining about problems with the water in the wake of a decision by a state-appointed emergency manager to switch the supply source to the Flint River to save money. Even now, many residents do not trust what flows from their taps, even as experts have reported that the water quality continues to get better.

For decades, Flint paid Detroit to have its water piped in from Lake Huron, with anti-corrosion chemicals added along the way. But in early 2014, with the city under the control of a state-appointed emergency manager, officials switched to Flint River water in an effort to cut costs.

State officials failed to ensure proper corrosion-control treatment of the new water source. That failure allowed rust, iron and lead to leach from aging pipes and wind up in residents’ homes. The catastrophe exposed thousands of children to high levels of lead, which can cause long-term physical damage and mental impairment, and water contamination also has been linked to the deaths of a dozen people from Legionnaires’ disease.

More than a dozen state and local officials have been charged with crimes in connection with the water crisis.

Read more: