The most-watched federal election of 2017 came to a stunning finish Tuesday with deep-red Alabama electing Democrat Doug Jones to the U.S. Senate—the first Democrat elected from Alabama in a quarter century and a major defeat for President Trump and Steve Bannon’s white nationalist wing of the party.

Jones, a former U.S. Attorney, defeated Republican Roy Moore, a former Alabama Supreme Court Justice who was removed from the state’s highest court for putting god above the U.S. Constitution and was repeatedly accused during the race of being a sexual predator who had targeted teenage girls.

Nonetheless, Trump embraced and endorsed Moore in the race’s final days, ignoring the advice of Republican Senate Majority Leader Mitch McConnell. On Sunday, the state’s other senator, Richard Shelby, who was elected as a Democrat in 1992 but became a Republican two years later, urged voters to reject Moore and write in another name. On Tuesday night, with 96 percent of precincts reporting, the write-in votes exceeded Jones’ victory margin.

With 2,138 of 2,220 precincts reporting, Jones had 49.5 percent or 638,000 votes, while Moore had 48.8 percent or 629,000 votes. The 20,000-vote balance, 1.7 percent of the total, were write-in votes for neither candidate.

“We have shown the country the way that we can be unified,” Jones said, framing his victory in terms of reconciliation and consensus. “This entire race has been about dignity and respect. This campaign has been about the rule of law.”

Moore did not concede defeat, however. In brief remarks to his supporters, Moore's campaign manager said that ballots from overseas military personnel had not yet been counted. (The state will not conduct a recount unless the victory margin is under 0.5 percent.)

"When the vote is this close, it's not over," Moore said. "It's not over and it will take some time."

The race was Moore’s to lose and his defeat is a prism that reveals much about the fissures in American politics, as well as the moral core in Trump’s win-at-any-cost politics. Just as Jones’ intense get-out-the-vote strategy clearly motivated the state’s African-American voters to turn out in record numbers for a special election, Moore’s controversies and Trump’s abrasiveness clearly prompted many Republicans not to vote.

Moore was such a caustic candidate that the Senate was poised to decide whether to swear him in or begin investigating his alleged sexual misdeeds as a stepping stone to his impeachment. Jones’ victory means the Republicans now control the Senate by a one-vote margin, 51-49, and that puts the body in play in 2018—an outcome many Democrats could barely imagine before Jones’ stunning victory.

The election was prompted by President Trump selecting Jeff Sessions to be U.S. Attorney General. The governor appointed Luther Strange, who lost a heated primary to Moore despite Strange’s support from establishment Republicans, namely McConnell. Steve Bannon, Trump’s former White House strategist, made it his mission to unseat Strange and embrace Moore, saying political outsiders like Moore are needed to defend and implement Trump’s white nationalist vision.

After Strange’s primary defeat, the Washington Post broke what a series of stories in which several women detailed Moore’s predatory sexual behavior, recalling incidents that took place when the women were teenagers. Moore vigorously denied the allegations and his campaign staffers attacked the women as opportunists and liars. The accusations took the campaign into a new orbit. Moore has long been a religious fundamentalist who relished his political martyrdom, but the allegations brought him into the center of a debate over toxic masculinity.

Moore and Bannon sought to portray the accusations as more meddling by elitist outsiders trying to tell upstanding Alabamians what to do—the old trope of locals resenting and rejecting outsiders. But the editorial boards of the state’s largest newspapers uniformly said that Moore was unfit to serve in the Senate—a statement that was not only echoed by the Jones campaign and Democrats, but by some of the state’s best known Republicans: Alabama Sen. Richard Shelby and former U.S. Secretary of State Condoleezza Rice.

As promising as these dynamics seemed to be for Democrats, the backdrop to this race was that Jones faced exceptionally long odds to become Alabama's first elected Democratic senator in decades. Even though Moore had “underperformed as a statewide candidate” in prior elections, as the Washington Post put it, “to win, Jones would have to turn out voters in droves from predominantly black areas, because Alabama’s geography—and politics—are sharply divided by race. And Trump supporters would have to stay home—or even flip for Jones.”

The pre-election polls have been all over the boards, noted Zac McCrary, a Democratic pollster based in Alabama. As he wrote early Tuesday, “The unusual turnout dynamics in a special election held 13 days before Christmas make it very difficult to know what the electorate will look like. Polls have been all over the place — just in the last 24 hours there have been public polls showing the race from +10 Jones to +9 Moore.”

The big question, he said, was about the likely racial composition of Tuesday’s voters.

“A lower African American turnout (say, comprising 24% of the electorate) makes things very tough for Jones — whereas a higher African American turnout (28% of the electorate) would make the math for a Jones victory much more manageable,” McCrary wrote. “Beyond African American turnout, can Jones reshape the white electorate and make it at least a touch younger, a touch more female, and a touch more highly educated than a usual mid-term electorate? That might be the key to hitting his ‘win number’ with whites. Under either scenario, Jones has to dramatically over-perform the basic Democratic DNA of the state.”

But that’s exactly what Jones did—he overcame the state’s political DNA because the Republicans, led by Bannon and Trump, put their weight behind one of the most controversial and ill-prepared candidates the national GOP has fielded in recent years. The reality of Moore’s sexual predations resonated to such a degree that Alabama Republicans stayed home in droves rather than vote for him.