Democrat Doug Jones defeated Republican Roy Moore in Alabama on Tuesday night, snagging a U.S. Senate seat in a upset and providing an appropriately unpredictable end to a bizarre race.

The election saw an uneasy coalition formed between President Trump and the Republican establishment only to be rebuked in the GOP primary; a sitting senator defeated in a primary runoff; a candidate refusing to withdraw despite a series of sexual-misconduct allegations involving teenagers; and, in the end, a Democrat robbing Republicans of a Senate seat in the deep-red Deep South.

With the last few precincts being counted, Jones was headed for a narrow but decisive victory over Moore, an archconservative culture warrior who was twice removed as chief justice of the state supreme court for defying federal judges. The race is at once an outlier—Moore was a uniquely flawed candidate—and the latest example of Democratic enthusiasm, and in particular African American engagement, spiking in backlash to the Trump era. Early analysis suggests Jones’s victory came on the power of black turnout that far exceeded expectations and white turnout that sagged in the face of a scandal ridden, bigoted GOP candidate.

The late polls in the race had shown a mixed picture, with results ranging from a 10-point Jones advantage to a 9-point Moore lead. Democrats viewed Jones, a mild-mannered attorney most famous for putting Ku Klux Klan members behind bars for the bombing of a church in Birmingham during the civil-rights movement, as an unusually strong candidate in Alabama, but there were intense worries about his ability to get African Americans to the polls. In the end, the results showed black voters making the difference.

The result is a blow to Trump, who saw his chosen candidates lose in both the primary and general election. The president first threw his lot in with Senate Majority Leader Mitch McConnell in backing Luther Strange, who was appointed to fill the seat of Jeff Sessions, who Trump had appointed attorney general. By many measures, Moore, a brash, controversial enemy of the establishment, was the more Trump-like candidate. Strange squeaked into a runoff, with Trump’s help, but then lost the race to Moore.

Despite his many flaws—in addition to his dual removals from the bench, Moore has a history of appalling statements about minorities, LGBT people, and others—the Republican seemed to have the general-election edge in Alabama, a solidly GOP state. Then, about a month before Election Day, the race was rocked by a Washington Post story detailing several women’s accounts of teenaged relationships with a 30-something Moore, including one who said that he had brought her to his home when she was 14 years old and guided her hand to touch his penis through his underwear. Soon, other women emerged with similar stories; one said he had sexually assaulted her. Several reports found that Moore may have been banned from a local mall in Gadsden, Alabama, for making teenagers uncomfortable.

The stories coincided with the #MeToo moment of past sexual misdeeds felling powerful men, and they sent Moore’s campaign into a tailspin. Most of the Republican Party abandoned Moore, with Senate Majority Leader Mitch McConnell saying Moore should withdraw, the Republican National Committee withdrawing funding, and National Republican Senatorial Committee Chairman Cory Gardner saying Moore should be expelled from the Senate if elected.

But after Thanksgiving, Moore seemed to recover his stride. He adamantly denied the allegations, in some cases claiming (against all evidence, including his own past statements) not to even know the women. Trump, himself the target of multiple accusations of sexual assault, backed Moore first timidly and then ferociously, and the RNC followed suit.

The final results show that Moore was no more immune to such allegations than most powerful men (the president excepted) have been in recent weeks. It also showed the power of black voters, and the Democratic Party’s surge in enthusiasm in the face of Trump’s agenda. It is, however, difficult to judge the broader significance of the Alabama result. While Jones’s win follows November elections in Virginia in which Democrat Ralph Northam won an unexpectedly strong victory in the governor’s race and Democrats made gains up and down the ballot, the specific circumstances of the Alabama race are largely unrepeatable.

But the result is clearly a disaster for Republicans. Alabama Governor Kay Ivey could have chosen not to have a special election until 2018, but moved it up. (She replaced Robert Bentley, who appointed Strange and then was forced to resign in disgrace—a strange story of its own.) Trump saw his chosen candidate defeated twice. Steve Bannon, his former strategist and Moore’s fiercest backer, also lost, and the result will intensify an ongoing battle for the identity of the Republican Party. Trumpist candidates like Moore have proven formidable in primary elections and vulnerable in general elections, even in deep-red states like Alabama. Jones’s victory narrows the Republican advantage in the Senate to a thin reed.

There is a silver lining for Republicans, though. A Moore victory would have kept the Senate seat in GOP hands, but it would have sent an unpredictable politician, implacably opposed to McConnell, unbeholden to the party, and tainted by sex scandal to Washington—a likely albatross around the GOP’s neck. With Moore as their candidate, Republicans had no good option in the race. It’s possible they ended up with the least worst option, though.