Just 30 hours before Senate Republicans passed their first major piece of legislation of the Donald Trump presidency, it looked as if they might instead have been heading toward familiar territory: grappling with another defeat at the tail end of a major legislative effort.

On Thursday, the party’s deficit hawks—who happen to be some of President Donald Trump’s loudest critics—were threatening to derail a tax-overhaul bill unless the text was revised to lower its impact on the national debt. GOP leadership began floating a proposal to offset hundreds of billions of dollars of tax cuts in order to placate their concerns. Piles of pizza boxes were hauled into Senate meeting rooms to feed lawmakers as they worked feverishly through the night.

By Friday morning, the drama had fizzled. A relieved Senate Majority Leader Mitch McConnell (R-KY) announced that he had secured the necessary votes for the bill—and without conceding much ground at all to Sens. Bob Corker (R-TN) and Jeff Flake (R-AZ), the chief deficit-scolds.

Sen. Mike Rounds (R-S.D.) told The Daily Beast that after certain parts of the bill were rewritten, Corker and Flake “really have” lost out on the substance of the bill. In short, they were outnumbered.

Concluding a marathon session that began Friday morning, the Senate in the early hours of Saturday morning finally approved the legislation by a vote of 51 to 49, with Flake reluctantly backing the effort and Corker opposing it.

The vote gave Senate Republicans a much-needed victory near the end of a year filled with bruising—even embarrassing—legislative setbacks. Their plan to overhaul the U.S. tax code will likely be sent to a conference committee where it will be reconciled with the House’s version. But the fact that it made it through the chamber—and that the party was able to cajole its disparate factions without making major changes to the text—indicates that final passage could happen before the end of the year.

As Vice President Mike Pence presided over the historic moment, Republicans gathered together toward the front of the chamber as Democrats quickly cast their “no” votes and hastily exited. After the vice president made the vote official, Republicans broke out into applause and shook hands.

The tension between Democrats and Republicans nearly reached a breaking point as lawmakers sat in the chamber for hours on end, concluding just before 2:00 a.m. Saturday morning.

Before lawmakers kicked off a marathon session on Friday evening known as vote-a-rama, Senate Minority Leader Chuck Schumer (D-N.Y.) disparaged the legislation—a major reworking of the American tax code that was unveiled in full just a few hours before its final passage—and impugned Republican leaders’ “decency” and “honor.” He made eye contact at certain points during his address with McConnell, who was sitting just 10 feet away and could be seen shaking his head several times.

For the first time in a long time—perhaps since the confirmation of Supreme Court Justice Neil Gorsuch—Republican senators were beaming about the outcome, both on its the substance of the bill and the political implications for the party heading into the midterm elections.

“It doesn’t get any better than this,” Sen. Johnny Isakson (R-GA) said, grinning, when asked about the victory in light of recent failures on health care reform. “I would not have wanted to re-do how we fell short on health care. … Next fall would’ve been rough on Republicans if we hadn’t done this.”

Whether they should be elated is another question. The nearly 479-page bill itself remains largely unpopular, in part because of its tax breaks for corporations—with the rate slashed to 20 percent—and the large sum it is expected to add to the federal deficit over the next 10 years. It also would undo Obamacare’s individual mandate, which will be welcomed by those who have to pay a penalty for not buying insurance, but is projected to result in 13 million more uninsured and a 10 percent hike in premiums.

Complicating matters further, the Joint Committee on Taxation (JCT) determined in a new report on Thursday that even with expected economic growth, the legislation would add $1 trillion to the deficit over 10 years.

Senate Republicans countered the report by calling the JCT analysis incomplete, and arguing that it provided a low-ball measure of projected economic growth. But it was startling enough that lawmakers such as Corker and Flake continued to seek fixes. They had worked feverishly to devise a so-called “trigger” mechanism that would raise taxes if economic growth didn’t meet projections. Many of their colleagues agreed.

“I believe the economic growth is going to happen. But I’d like to have a backstop in case,” Sen. James Lankford (R-OK) told The Daily Beast just before the vote. “This is walking on a wire without a net, which if you hit it right is no problem.”

But Corker was the only Republican to vote against the bill, citing his longstanding belief that the legislation could balloon the national debt without sufficient assurances that the economy would grow. His decision came a day after the Senate parliamentarian said the chamber’s budget rules did not allow for a trigger mechanism, causing a sir on the Senate floor and forcing GOP tax writers to head back to the drawing board.

“I just think the caucus wanted to go in a different direction. It was just a difference of focus,” Corker told The Daily Beast of his decision to break with his party. The Tennessee senator has argued that the bill could have been written in a way that is fiscally responsible and also boosts the economy. But neither Corker nor his colleagues seem to be holding grudges.

“Every effort was made to do a trigger,” Isakson told The Daily Beast. “It wasn’t that his problems weren’t addressed; it’s that his problem wasn’t addressable from a parliamentary standpoint.”

Flake ended up backing the bill after receiving what he called a “firm commitment from Senate Leadership and the administration to work with me on a growth-oriented legislative solution to enact fair and permanent protections for DACA recipients,” referring to the program that shields children of undocumented immigrants from deportation. The Arizona Republican supports the DREAM Act as a way to codify the program, which Trump rescinded earlier this year. But shortly after Flake emerged to tout his deal, the White House downplayed its significance, saying only that they would have a discussion on DACA and that Flake would be part of it.

Republican leaders put in sweeteners for other lawmakers, too.

Sen. Susan Collins (R-ME) a key, early holdout on the legislation, decided to support the bill after securing a commitment from McConnell that the Senate would take up the bipartisan Alexander-Murray legislation, which would ostensibly mitigate the impacts of repealing the individual mandate. Sen. Ron Johnson (R-WI) announced he’d support the measure, too, after securing an increase in the deduction allowed for pass-through business owners, from 17.4 percent to 23 percent.

A fact sheet tweeted out by Sen. Claire McCaskill (D-MO) showed that a number of other amendments were added at the last minute, too. Sen. Pat Roberts (R-KS) secured an amendment to tax agricultural and horticultural co-ops similarly to other farming businesses. Roberts chairs the Senate Agriculture Committee. Sen. Lisa Murkowski (R-AK), secured one to modify treatment of Alaska Native Corporations.

The full extent of what these amendments meant wasn’t exactly clear, however, as no final legislative text was available even late on Friday evening. The final text was posted online just after 10:00 p.m. McCaskill said she was only able to get the list from “lobbyists downtown.”

On Friday morning, Sen. Orrin Hatch (R-UT), the chairman of the Senate Finance Committee, told The Daily Beast that “there can be changes even now,” with a vote planned for later in the day. After a midday meeting, nearly a dozen Republican senators told The Daily Beast that they had seen the revised bill and were comfortable with the changes.

But as day turned into night, the legislation was still being tweaked. Democrats pointed to some edits in the legislation that were scribbled by hand, and the final product wasn’t made public until hours before the final vote. Lankford told The Daily Beast that the handwritten provisions were done for the sake of speed, and would be printed into the final bill text. Still, the episode underscored Democrats’ objection that the final version of the legislation was not made public until just before the vote. Republicans were quick to brush off those criticisms.

“We’ve had a version for a long time. All we’re really looking at is the red-line changes,” Lankford said. “So I’m sure there’s going to be people saying on the floor, as there already have been, saying, ‘oh my gosh, we just got this giant bill.’ Everybody’s had the giant bill for a long time.”