Tucked quietly into the most recent congressional measure to keep the government open was the most sweeping and ambitious piece of child welfare legislation passed in at least a decade. It’s an attempt to reshape the entrenched foster care system as a raging opioid epidemic swells the population of children in need.

The measure overcame the opposition of group homes, which pocket thousands of dollars per month for each child warehoused in their custody. The Family First Prevention Services Act upends the funding structure for the child welfare system by allowing states to use federal matching funds for programs addressing mental health, substance abuse, family counseling, and parent skills training — to keep at-risk children from entering the foster care system in the first place. It’s meant to help families stay together.

Most new programs are funded by specific amounts of money, which makes them vulnerable to cuts or expiration in the future, but the new law amends the Social Security Act to open up funding for families at risk of entering the foster care system. That means major funding will be available in states willing to take advantage of the new federal money.

The law is also designed to deter the use of group homes, which profit from the children they take in and shuffle through, by limiting federal funding for congregate care and reducing the number of kids going into the system at all. At a Senate HELP Committee hearing on Thursday, William Bell, president of Casey Family Programs, noted that for every $7 spent on foster care, there is only $1 spent on prevention.

The law seeks to rebalance a particularly difficult dynamic at play in the foster care system. Imagine the situation from the perspective of a caseworker: You see a parent struggling with her housing situation, holding down several low-paying jobs, and perhaps you suspect some substance abuse issues. There are pamphlets you can hand out, organizations like food pantries or diaper banks you can recommend for some elementary services, but beyond that, what can you do?

The sole significant action available is to break the family up and put the child in foster care, stuffing them in a group home. If you don’t and something goes wrong, it’s on you. If you do put the child in a group home and something goes wrong, well, that’s the fault of the group home.

As the law was drafted in Congress, lawmakers heard testimony about how the lack of options for caseworkers is one of the great stressors associated with the job — one that has, not coincidentally, extraordinary turnover. It sets up a system in which all incentives lead toward breaking up the family, even though studies show doing so produces far worse outcomes.

The new law balances that incentive by giving caseworkers some meaningful things they can actually do. Now they’ll be able to offer addiction treatment and counseling, parenting support services or moving the child in with close family. The latter option — allowing the child to live for a time with grandparents or aunts and uncles — has long been the most commonsense approach, but has never been truly supported or encouraged by public policy. Now, with the Family First law, it is.

The law also takes some practical steps not to set up new disincentives. Much of government action on the ground level is driven by how it influences funding flows. In order for a state to recoup federal money for foster care services, for instance, there are income thresholds involved. (Not many rich children wind up in foster care, but it’s part of the law nonetheless.) The new law says that if a child’s relatives attempt to take the child in and for whatever reason it doesn’t work, the child is still eligible for federal support. If that weren’t made clear, caseworkers might be reluctant to let a grandparent try to take a child in, for fear that the time spent with a relative with a higher income would make them ineligible for federal help down the road. Those are the kinds of backward incentives that had long been a blight on the country’s foster care system, but are finally being addressed.

Sen. Orrin Hatch, Republican chair of the Finance Committee, who co-sponsored the legislation with ranking Democrat Sen. Ron Wyden, said on the Senate floor on Thursday the legislation would “help keep more children safely with their families.”

In 2016, Hatch and Wyden tried to push nearly identical legislation but it failed to move through the Senate after a Baptist group home network in North Carolina pressured its Senate delegation to go against it. The Baptist Children’s Home of North Carolina, along with other providers across the country, voiced objections this time around too. Most of the resistance to foster care reform came from group home networks — the legislation would reduce the number of kids entering the system, therefore interfering with their revenue model of stowing away children. But there was also opposition from California and New York, where the child welfare community says the federal solution will undermine state-based solutions they embarked on years ago.

Despite California’s back-home ambivalence, Democratic Minority Leader Nancy Pelosi was a leading advocate of the bill in the House and pushed to have it included in the spending bill. The approach, by elevating families and promoting individual development, was also attractive to conservatives. “Speaker Ryan and House Republicans have consistently pushed for improvements to our welfare system, and are pleased that the budget bill included reforms to our child welfare system,” said AshLee Strong, a spokesperson for House Speaker Paul Ryan, in a statement to The Intercept. “We will continue working to improve our welfare system and pursue workforce development reforms that get people the skills and training they need to find employment.”

In a fortunate political twist for foster children, North Carolina Sen. Richard Burr’s opposition may have created the conditions that made ultimate passage possible. Media coverage of his lonely stand on behalf of home-state group homes made the issue understandable for the first time for the popular press. Even Teen Vogue, then at the beginning of its run as the organ of the “woke resistance,” weighed in against Burr. That helped Democrats decide which side of the bill they wanted to be on and unified the caucus in favor of reform — even Democrats from balking states like California and New York. Without that consensus, the bill couldn’t have made it into the spending package.

“The Family First Prevention Services Act will usher in the most significant improvements to the child welfare system in decades and provide real help to families to fight the opioid epidemic,” Wyden said in a statement. “We owe our most vulnerable children the best chance to stay with their families when it’s safe to keep them at home and the highest standards of care to protect children who are already in foster care.”

Children are taken from their families in cases of abuse but more often than not, Sandy Santana, executive director for Children’s Rights, said, kids enter the foster care system because of neglect that stems primarily from issues of poverty or substance abuse.

“With the opioid epidemic, more and more kids are coming into the system because their parents are dependent on opioids,” Santana said. “For the government now to redirect funding for substance abuse and preventative services to keep those kids that are at risk of entering foster care with their families is a big, big deal.”

In 2012, there were 397,000 children in foster care, but by 2015, the number rose to 428,000, according to data from the U.S. Department of Health and Human Services. The 8 percent increase was largely due to the opioid epidemic, and states hit harder by the growing abuse of opioids report the crisis is straining their foster care system and getting worse, but exact data is not available yet.

The idea behind this is to help families early on before costlier interventions are needed, Santana said, adding that “if you look at the foster care system for some kids, it’s worked well, but there are many broken foster care systems in the country and kids are moved from place to place, sometimes they’re abused and neglected in the very system meant to protect them.”

The Family First law also incorporates rigorous assessments to make sure that a child actually has a continuing need to be in a group facility, and if not, the child is sent back to a family foster home so they don’t languish in group care.

In October, the Senate Finance Committee released its investigation into one of the largest for-profit providers of foster care services and found the children in its care had been dying at alarming rates over the past decade, with no one investigating the deaths. The committee found that 86 children had died in the company’s care over a 10-year period, and the firm had conducted internal investigations in only 13 cases. The committee found the MENTOR Network did not investigate fatalities; the vast majority of children who died were not the subject of internal investigations even when the death was unexpected, and pending autopsy reports were excluded from files.

The company told the committee it serves “significantly more children and youth with heightened risk factors relative to others in foster care, and sustains child mortality rates that are comparable with national norms.” But the panel noted its death rate among foster children was found to be 42 percent higher than the national average.

Now, under the new reforms, group homes will be required to more fully document the steps they take to track and prevent child maltreatment deaths, as well as explain how they are implementing a plan to deal with this problem.

Governing by stopgap funding measure has been a rough ride for children the past several months, but they have finally started to see programs aimed their direction reauthorized, along with billions for the military and disaster relief.

The government partially shut down and reopened hours later when President Donald Trump signed the massive budget deal into law early Friday morning. The budget deal includes a $160 billion military spending boost, a long-awaited $89 billion in disaster relief, and funding for lapsed health programs that until this week, were scraping by with leftover funds. The Maternal, Infant and Early Childhood Home Visiting program won’t have to worry about money for the next five years and the Special Diabetes Program for two. The Children’s Health Insurance Program, which covers 9 million kids, went from unprecedented crisis to being funded for the next 10 years.

Correction: Feb. 16, 2018 An earlier version of this story misquoted Casey Family Programs President William Bell at the Senate HELP Committee hearing on February 8. Bell’s quote has since been updated.