In the wake of the 2018 midterms, as a blue wave swept a Democratic majority into the US House of Representatives, Nancy Pelosi’s plan to take back the speaker’s gavel appeared to hit some rocks. A group of moderate-to-conservative Democratic members of Congress, led by Representatives Seth Moulton and Tim Ryan, were vowing to topple her, as they tried, and failed, to do in 2016. This year, they told reporters early this month, they had “20ish” solid votes against her, including some “freshmen.” Just as that bid took off, 2018’s most famous freshman, Alexandria Ocasio-Cortez, joined a protest at Pelosi’s office to demand faster action on climate change. Cable news ran the scene of Ocasio-Cortez addressing a raucous crowd in Pelosi’s office on a loop, illustrating stories about Pelosi’s potentially losing her grip on the gavel.

Meanwhile, political Twitter conflated the two uprisings as related. Karl Rove was gleeful. For a moment the conventional wisdom spread that the new wave of Democrats elected in November might wash Nancy Pelosi away.

It did not. Pelosi won the support of the House Democratic Caucus on Wednesday with a vote of 203-32, with three abstentions. In 2016, she beat Ryan 134-63. Ryan and Moulton vow to take their fight to the floor, when Democratic nominee Pelosi faces off against Republican nominee Kevin McCarthy in the official vote for House speaker January 3.

It’s true that Pelosi will need to win the support of at least 15 of those who voted “no” or abstained on Wednesday. But Pelosi sources say her strategy has always been to expect some defections in caucus—and then expect most of those defectors to back her against McCarthy on the floor. Moulton may be more wounded by the battle than Pelosi, his crusade against her having ignited a backlash in his district that could force him to fight for his seat, even as he was rumored to be eyeing a White House run.

So how did Pelosi do it?

In fact, the office protest that so many reporters depicted as damaging to the Democratic leader was a turning point in her winning back control. She defused the situation by telling the Capitol Police not to make any arrests and praising the protesters. “Deeply inspired by the young activists & advocates leading the way on confronting climate change,” she wrote on Twitter, and reaffirmed her October promise to establish a climate-action committee.

Ocasio-Cortez replied with equal grace. “Should Leader Pelosi become the next speaker of the House, we need to tell her that we’ve got her back in showing and pursuing the most progressive energy agenda that this country has ever seen,” she tweeted, leaving the door open to supporting the San Francisco Democrat’s leadership bid.

Thus Pelosi opened a wedge between newly powerful progressive insurgents and her conservative opponents, who were widely blasted on Twitter as #fivewhiteguys, referring to ringleaders Moulton, Ryan, Colorado’s Ed Perlmutter, Illinois’s Bill Foster, and Oregon’s Kurt Schrader.

Sure, there were a few women in the group behind Moulton and Ryan—most notably New York Representative Kathleen Rice, California’s Linda Sanchez, and, for a time, former Congressional Black Caucus chair Marcia Fudge. And there were freshmen, too: Among the most notable were New Jersey’s Mikie Sherrill, Virginia’s Abigail Spanberger, and New York’s Max Rose, which irritated some progressives who worked hard to help those Democrats flip Republican seats.

But the inspiring first-year progressive insurgents whose wins shocked the Democratic political establishment—and even some progressives—wound up backing Pelosi as speaker even before Wednesday’s caucus vote. Ocasio-Cortez endorsed her a little over a week after she joined the office protest—“All the challenges to Leader Pelosi are coming from her right, in an apparent effort to make the party even more conservative and bent toward corporate interests,” she wrote on Twitter—as did Minnesota’s Ilhan Omar, one of two Muslim women elected this month. Three new progressive women leaders who had expressed opposition to Pelosi during their campaigns—Boston’s Ayanna Pressley and Connecticut’s Jahana Hayes, the first black congresswomen from their states, as well as Detroit’s Rashida Tlaib, Congress’s second Muslim woman, came around too. In a statement, Pressley backed Pelosi for her “progressive track record and her express commitment to bring a background check bill to the floor.” Tlaib told WBUR, “I think the American people need to understand: There is no one else running against her in the Democratic caucus. “

What turned the tide? Pelosi’s savvy, sympathetic reaction to the protest in her office helped. Equally important was a meeting she held a few days later with Pramila Jayapal and Mark Pocan, co-chairs of the Congressional Progressive Caucus, which is set to grow by at least 20 members next year, to make up roughly 40 percent of the Democratic caucus. Pelosi promised progressives proportional representation on influential committees like Appropriations, Ways and Means, Intelligence, Energy and Commerce, Financial Services and Intelligence, and agreed to add staffing and new leadership positions to the progressive-leaning Democratic Policy and Communications Committee. Pocan and Jayapal released a statement praising Pelosi; hours later Indivisible, MoveOn and revered healthcare activist Ady Barkan announced their support for Pelosi. The next day, so did Jayapal.

“No one can really doubt Pelosi’s progressive chops,” Jayapal told Politico in an interview. “And I do think, for the next two years, as we lead into 2020, and are coming off this big wave, we need someone who is smart and strategic and has done this.” Endorsements by progressives like Ocasio-Cortez, Pressley, Hayes, and Omar came days later. “I think it was very important for progressives to take a unified position and also to work an inside/outside organizing strategy,” Jayapal told me, with allied groups holding back their Pelosi endorsements until she and Pocan announced their agreement.

Pelosi also gained traction from the revelation that the #fivewhiteguys and their allies were backed by the slippery “No Labels” group, which essentially capes for Republicans while pretending to be nonpartisan, and its allies in the so-called “Problem Solvers Caucus,” 12 Democrats and 12 Republicans who meet occasionally to fantasize about bipartisan solutions to congressional gridlock. Nine of the 12 Democrats in that bipartisan caucus were among Pelosi’s leading critics.

The Intercept revealed that undead Democratic consultant Mark Penn and his wife, Nancy Jacobson, who runs No Labels, were forces behind the plot to depose Pelosi, scheming to make her the unattractive face of the Democratic left. I can’t describe Penn any better than Esquire’s Charlie Pierce, who calls him “the Typhoid Mary of bad political strategy…the Patient Zero of terrible political ideas. This guy couldn’t get Jesus elected to a parish council.” Penn is widely credited with Hillary Clinton’s failed 2008 primary campaign. This push failed as well.

The biggest problem was that the #fivewhiteguys could never find someone to challenge Pelosi. After Ryan lost to her badly in 2016, he and Moulton seemed to realize that a white man challenging the most powerful woman in American politics, after an election that brought a historic wave of new Democratic women into the House, was a bad look. Ryan embarrassed himself by telling reporters there were plenty of “competent females” in his caucus, as though he was talking about livestock. But they couldn’t find a front woman for their crusade. For a time, Marcia Fudge expressed interest. But the Congressional Black Caucus stood firm behind Pelosi, and after a meeting with the leader, Fudge announced she’d back her, having secured a promise she’d be appointed to chair a reinstated subcommittee on elections, to advance the cause of voting rights. Then two more of 16 House members who signed a letter opposing Pelosi’s speakership joined Fudge in announcing they’d changed their minds and would back her.

In the end, Pelosi even managed to split the Problem Solvers caucus from Moulton, Ryan, and Rice, by agreeing to a package of rules reforms, the most notable of which gives preference to moving bills with bipartisan backing, and allows any measure with 290 supporters to be moved to a floor vote. It was a far cry from an earlier reform proposal that would have given more power to Republicans and conservative Democrats. Jayapal can live with it. “You’d need 65 Democrats plus everyone on the other side,” the Seattle congresswoman told me. “That’s a high enough threshold that you won’t have Republican bills supported by only a handful of Democrats coming to the floor.”

Thus Pelosi entered Wednesday’s caucus election so confident of victory, she had the internal ballot modified to include a “no” vote—so that any Democrats who had promised their constituents they’d vote against her as speaker could keep their promise. As late as Wednesday morning, Moulton was insisting the number of Democrats voting “no” in caucus would prove Pelosi didn’t have the votes to become speaker. But the newly elected Jahana Hayes of Connecticut—who, again, declined to support Pelosi during her campaign—rejected that strategy. “I’m not sure I understand it,” she said. “ ‘No’—and replace with what? If we are truly attempting to be solution-driven, then when you vote no, present a solution or offer up some alternative or a candidate.”

A source close to Pelosi said Moulton’s phantom-candidate strategy guaranteed Pelosi’s reelection. “Many new members came thinking the ‘Never Nancy’ team had a captain, but their failure to actually run a candidate soured people. Imagine you’re a new member and they tell you, ‘Push out the Leader then we will offer you another candidate.’ That’s not how democracy works!”

It’s true that Pelosi wound up with 32 no votes and three abstentions, and if all of those Congress members fail to support her on the floor, she would be unable to get 218 votes and become speaker. But back in 2016, when 63 Democrats voted against her in caucus to be minority leader, only four opposed her when the vote came to the floor. Jayapal expects most of the Democrats who voted against her in caucus to support her on the floor, when the alternative is to elect a Republican.

Ryan and Moulton vow Pelosi won’t get the votes she needs January 3, but they’ve fallen short before. Meanwhile, Moulton might have to tend to the home fires. His crusade against Pelosi has awakened opposition in his district; he played defense at a rowdy town-hall event just last week. “There are enough angry progressives, particularly women in Massachusetts and across the country, who will work hard to see that he does not advance any further politically,” says constituent Isa Leshko, an artist who helped organize the town-hall protest. “We are already planning our next steps to hold him accountable to progressive voters and to pave the way for a strong primary challenger in 2020.”