The first woman to wield the gavel of the House of Representatives is on track to secure it for a second time on Thursday, as a rebellion against her from within her own ranks was crushed from election day until now.

As the onset of the 116th Congress approaches, convening at noon on Thursday, Jan. 3, 78-year-old Rep. Nancy Pelosi (D-CA) is the only declared Democrat candidate for Speaker of the House. Barring any unforeseen circumstances or last-second shenanigans by Democrat rebels against her, Pelosi will win back the Speaker’s gavel on Thursday afternoon–putting her in the driver’s seat on negotiations with President Donald Trump on everything from the ongoing government shutdown to major federal policy decisions on just about every issue.

Pelosi served as Speaker from January 2007 until the beginning of January 2011, for two Congresses when Democrats held the majority at the end of the George W. Bush administration and beginning of the Barack Obama administration, but she has been stuck in the minority since Republicans took control of Congress in the 2010 midterm elections.

The GOP held control in the House for eight long years, first with former Rep. John Boehner (R-OH) as Pelosi’s immediate successor as Speaker and then, after Boehner lost an internal Republican Party rebellion of his own with soon-to-be-former Rep. Paul Ryan (R-WI), as Speaker.

For nearly a decade, Pelosi clung to hopes of a return to power from her perch as Minority Leader–the head of the House Democrat Conference–but nearly was denied the opportunity when her massive unpopularity forced Democrat candidates to pledge opposition to her Speakership candidacy in exchange for support from the public. According to NBC News, a whopping 58 Democrats–11 incumbents and 47 new candidates who won their party’s nominations for House seats–pledged campaign trail opposition to Pelosi’s re-ascent to the Speakership.

There are technically two votes for the Speakership. The first is in conference, where members of a party select whom their candidate will be. Democrats already selected Pelosi as their conference’s candidate with a late November vote of 203-32 with three blank ballots and one absent member. The second, and more consequential, vote is on the floor of the House of Representatives. The first order of business of a new Congress is for the House to elect a Speaker. So, when the 115th Congress disbands and the 116th Congress is sworn in, before the House can do anything else, the chamber must elect a Speaker.

For a candidate to win the Speakership of the House, that person must receive a majority of those present and voting for a person. Members of Congress can vote for formally introduced candidates–for Democrats that will be Pelosi and for Republicans that will be Rep. Kevin McCarthy (R-CA), the current Majority Leader who will become the House Minority Leader on Thursday when Democrats take over–or they can vote for someone not listed. The candidate can be a member of the House, but does not have to be–the Constitution is very nonspecific on the requirements for candidates for election to be Speaker of the House–though there has never been a Speaker who is not a member of the House.

For someone to win the vote, they need to win a majority of those members who voted for a candidate. Voting “present” technically is a thrown-away vote, as it does not count toward the number of votes cast for candidates, and thereby lowers the total threshold. Assuming all 435 members of the House are present and vote for a candidate, that would mean the winner needs 218 votes. If anyone is absent or votes “present” and not for a candidate, that lowers the number of votes a winner needs one member for each two members voting “present” or absent from the vote.

If no candidate wins a majority on the vote, the voting goes to a second round–and third, fourth, fifth, sixth, seventh, eighth, et cetera–until a majority of the House elects a Speaker.

It is in this vote that Pelosi faced the most potential peril before now. After the midterm elections, when it was clear Democrats were taking the majority in the House next year, 17 Democrats banded together to sign a letter pledging to oppose Pelosi, not just in the intra-conference vote but on the floor of the House as well.

“We promised to change the status quo, and we intend to deliver on that promise,” the 17 Democrats wrote in their letter. “Therefore, we are committed to voting for new leadership in both our Caucus meeting and on the House floor.”

The incoming House Democrat majority will have 235 members, while Republicans will have 199 members and there will be one vacancy in North Carolina’s yet-to-be-resolved ninth congressional district. That means if those 17 had held together and added one more to make their number 18, they would have had the votes to stop Pelosi from ascending to the Speakership at least on the first ballot–forcing a chaotic process and at least a second ballot.

But in the lead-up to, and aftermath of, the intra-conference nominating vote, Pelosi maneuvered swiftly to win several of those critics back.

The highest profile signer of the letter was former Congressional Black Caucus chairwoman Rep. Marcia Fudge (D-OH), who had even publicly floated potentially running against Pelosi. But, within a week of Fudge’s opposition to Pelosi becoming public and an interview she gave considering a Speakership bid herself, Fudge completely reversed course and actually endorsed Pelosi for Speaker.

How that happened is one for the ages: All of a sudden, when she opposed Pelosi, a letter she wrote seeking a lenient sentence for a Cleveland-area disgraced former judge convicted of beating his wife resurfaced. The timing could not have been better for Pelosi: that same judge had just been arrested on suspicion of brutally stabbing his wife to death after release from prison on the domestic violence charges for which Fudge sought a more lenient sentence for him. The judge has since been formally charged with his wife’s murder, and pled not guilty, and Fudge’s nascent bid for the speakership ended before it even took off. She dropped out of the running, took a token subcommittee chairmanship, and then endorsed Pelosi for speaker.

Next, Pelosi flipped Rep. Brian Higgins (D-NY) with a promise to focus on his priorities of healthcare and infrastructure–things Pelosi already planned to do anyway.

Then came the big guns: With a loose promise that the 78-year-old Pelosi would term limit herself, something she intended to do anyway, Pelosi flipped another group of rebels into backing her. Rep. Ed Perlmutter (D-CO), a senior leftist Democrat from Colorado who had previously been quoted in Politico publicly organizing against Pelosi, flipped and endorsed her, thereby rescinding his signature on the letter.

So did another leader of the rebellion: Rep. Tim Ryan (D-OH) backed off his opposition to Pelosi with that deal:

After he signed a letter vowing to vote against her, Democrat Rep. Tim Ryan says he will now support Rep. Nancy Pelosi for Speaker of the House. https://t.co/1dfiIdJqJd — Kyle Morris (@RealKyleMorris) December 13, 2018

Now, Ryan says the Democrat conference is unified behind her:

WATCH: @RepTimRyan (D-OH), who had previously called for new leadership, said that House Democrats are “absolutely” united behind Pelosi. https://t.co/K4ekX13crl — NTK Network (@NTKNet) January 2, 2019

Rep. Seth Moulton (D-MA), who called for Pelosi to step aside in a CNN op-ed and was one of the most public organizers of the seemingly failed coup attempt, also flipped and backed her candidacy. Several of the other signers, including Reps. Stephen Lynch (D-MA), Filemon Vela (D-TX), Linda Sanchez (D-CA), and Bill Foster (D-IL), also flipped with the term limits pledge.

Some of the other signers–and others who pledged to oppose Pelosi but did not sign the letter, which was circulated by Rep. Kathleen Rice (D-NY)–are still in opposition to Pelosi. But without the core of their group, which Pelosi won back, the rebels likely do not have the votes to stop her from winning the gavel.

it still remains to be seen what happens on the floor of the House on Thursday afternoon, and anything could go wrong for Pelosi before the vote happens, but for now it looks like she maneuvered her way through this rebellion and completed one of the biggest political comebacks in history against all odds.