Doug Jones has won the Alabama special election, according to multiple news outlets, the first Democrat in the state elected to the US Senate in more than 20 years. He delivers the Democrats a coveted Senate seat, shaving the GOP majority to 51.

Jones defeated controversial Republican candidate Roy Moore. Moore, a former chief justice on the Alabama Supreme Court, has courted controversy throughout his career, but retained a loyal base of conservative and evangelical followers that seemed likely to carry him safely to a Senate victory in deep-red Alabama.

But with a month left in the campaign, the Washington Post reported on the stories of four women who accused Moore of preying on them as teenagers, including one woman who said she was 14 at the time, when Moore was a prosecutor in his 30s. Additional allegations followed, upending the race.

Jones, a former US attorney who prosecuted two Ku Klux Klan members for the bombing of a Birmingham Baptist church in 1963 that killed four black girls, ran a low-key, centrist campaign that focused on the “kitchen table” issues of health care and economy. He campaigned hard against Moore but largely avoided the national spotlight.

And national Democrats, skittish about appearing too involved in the race, mostly stayed away from the state, with the exception of last-minute stumping by Sen. Cory Booker (D-NJ) and a robocall from former President Barack Obama.

Jones’s unlikely victory in Alabama counts as another positive signal for Democrats heading into 2018. They never banked on winning this Alabama seat — but desperately needed it. The electoral map favors Republicans in the midterms, who are defending eight seats to the Democrats’ 26. Jones will not face reelection until 2020, meaning Democrats require just two more seats to flip the Senate.

Even before 2018 arrives, Jones strengthens the Democratic position in the Senate, where Republicans can barely afford to lose any votes. Defecting Republicans defeated legislation repealing Obamacare. The Senate’s version of the GOP tax plan passed, but it was close.

Jones’s triumph is also a lesson to Democrats in the post-Trump era: They can’t afford to phone in any campaign. The party should look to be competitive even in an uphill race. In this case, putting up a viable candidate paid off.

Of course, Democrats didn’t win a typical election. This is Alabama; the conditions had to be just so to get Jones across the finish line. The misconduct allegations weighed down the race, especially with Congress and the entire country in the middle of a painful reckoning with sexual harassment and abuse. And Moore’s extreme views likely turned off a lot of moderate Republicans and independents even before the revelations about his past.

More detailed polling data will reveal whether dissatisfaction with Moore depressed turnout or prompted enough people to cast write-in ballots, or if enthusiasm for Jones helped to determine the final outcome.

Alabama voters have grabbed a Senate seat from the Republicans, but they also solved a big problem for the party. The GOP will now avoid a lengthy Senate ethics investigation into Moore, or possible expulsion proceedings that could have further divided the party and distracted from its legislative agenda.

Moore’s defeat deals a blow to Steve Bannon and his war on Senate Majority Leader Mitch McConnell and the establishment. Bannon and his faction won’t disappear, but the Alabama election might have proved, once again, that Trumpism has its limits.