The Senate passed a major criminal justice bill, the FIRST STEP Act, by a vote of 87-12 Tuesday night.

The bill still must go to the House, which passed a different version of the bill in May, but today's vote was an emotional victory for advocacy organizations and a bipartisan group of lawmakers who have worked for years, with little success, to shepherd a criminal justice bill to the Senate floor. Sens. Mike Lee (R-Utah) and Cory Booker (D-N.J.) hugged each other as the final votes rolled in.

Getting to that point required unending back-and-forth negotiation, numerous rewrites, rallies in the sweltering D.C. summer heat, convincing the notoriously flighty president to back the bill, and then convincing Senate majority leader Mitch McConnell (R-Ky.) to put it on the Senate schedule—not to mention holding together a coalition of liberal, conservative, and evangelical groups.

"Historic" and "once-in-a-generation," senators called the White House-backed bill.

Senate Judiciary Committee chairman Chuck Grassley (R-Iowa), a former opponent of criminal justice reform who became one of the chief authors of the bill, said in a statement that its passage was "an important victory in our years-long effort, which has resulted in a broad bipartisan recognition of the need for reforms."

In reality, the FIRST STEP Act is large but modest, and filled with numerous exceptions to gain the backing of law enforcement organizations, whose support was critical in gaining Trump's endorsement. The bill would expand reentry and job training opportunities for federal inmates, ban the shackling of pregnant inmates, and require inmates to be housed within 500 miles of their families, when possible.

It also includes four changes to federal sentencing law that would reduce some mandatory minimum sentences, expand judges' discretion under the so-called safety valve, and make the reductions to crack cocaine sentences under the Fair Sentencing Act of 2010 apply retroactively to current inmates.

Among some Senate Republicans, though, the bill was radical, and so it seemed somewhat miraculous when McConnell voted to retroactively reduce the sentences of an estimated 3,000 federal inmates serving time for crack cocaine offenses.

Senate Democrats unanimously voted for the FIRST STEP Act, including progressives such as Cory Booker (D-N.J.) and Kamala Harris (D-Calif.). Left-leaning groups have been skeptical of the bill, and many opposed the House version, which lacked any sentencing reforms. However, the American Civil Liberties Union (ACLU) ultimately endorsed the Senate version.

"We applaud the bipartisan group of Senators who were willing to listen to advocates and include important sentencing reforms that will grant thousands of currently incarcerated people a second chance," Jesselyn McCurdy, ACLU deputy legislative director, said in a statement. "People's lives are at stake."

The main threat to the bill came from Sen. Tom Cotton (R–Ark.), a staunch supporter of mandatory minimum sentences who called the bill a "jailbreak."

"A number of serious felonies, including violent crimes, are still eligible for early release in the version of the bill the Senate will vote on in a matter of days," Cotton wrote in a National Review op-ed Monday. "In short, the First Step Act flunks their basic test to protect public safety."

Cotton and Sen. John Kennedy (R–La.) introduced three amendments that critics said would gut the legislation. Those amendments would have excluded wide categories of felons from being eligible for the bill's provisions.

"Rather than try and fix [laws] we're going to give almost unfettered discretion to the bureaucrats and the Bureau of Prisons to fix our mistakes," Kennedy said on the Senate floor. "It's like putting paint on rotten wood."

Another amendment introduced by Cotton and Kennedy would have required wardens to notify crime victims when an inmate received credit toward an earlier release under the bill's provisions.

However, several victims rights groups opposed the amendment, saying it didn't allow victims to opt out of being notified.

"This proposed amendment would notify all survivors, whether they choose to be notified or not," the group Fairness, Dignity & Respect for Crime Victims & Survivors Project said in a statement. "This takes away the victim's autonomy to choose to remain informed and notified, and autonomy is a key tenet of trauma-informed victim/survivor assistance."

Cotton's insistence on introducing an amendments angered criminal justice advocacy groups, who have repeatedly accused him of negotiating in bad faith to kill the bill.

Those groups, such as Freedom Works, American For Prosperity, the ACLU, and Families Against Mandatory Minimums, went on a lobbying blitz Monday to urge senators to vote down the Cotton and Kennedy amendments.

All three amendments failed by wide margins.

"Thanks to an unwavering bipartisan supermajority in the Senate, the First Step Act is one step closer to becoming law," Mark Holden, the general counsel of Koch Industries, said in a statement. "We thank each Senator who did the right thing for our country by passing the bill without the amendments intended to undermine it."

The House must now quickly take up the bill during the remaining time in the lame duck session. House speaker Paul Ryan tweeted his support for the bill shortly after the Senate passage: "Criminal justice reform is about giving more Americans a chance at redemption," he wrote. "The House looks forward to sending it to the president to become law."

The headline and text of this article have been updated to reflect the passage of the FIRST STEP Act.