Controversial former judge Roy Moore won Alabama’s Republican Senate nomination on Tuesday over incumbent Luther Strange in a major setback for President Donald Trump and Senate Majority Leader Mitch McConnell.

Trump traveled to Alabama just last Friday to headline a raucous rally touting Strange. A McConnell-aligned super PAC spent millions of dollars to help the senator, who was appointed earlier this year to the seat vacated by U.S. Attorney General Jeff Sessions. And in a final push on behalf of Strange, Vice President Mike Pence campaigned with him at a closing rally Monday night in Birmingham, the state’s largest city.

It all turned out for naught, with Moore’s victory dealing a bitter blow to the GOP’s establishment wing. His win comes at a critical time as Republican lawmakers in Washington struggle to unite around key legislative goals on health care and taxes.

The Senate Leadership Fund, a super PAC tied to McConnell that spent vast sums on Strange’s behalf, conceded defeat before the results were officially called.

“While we were honored to have fought hard for Big Luther, Judge Roy Moore won this nomination fair and square and he has our support, as it is vital that we keep this seat in Republican hands,” Senate Leadership Fund President Steven Law said in a statement.

Strange was seen by McConnell and other GOP Senate leaders as a reliable member of their caucus. Moore, meanwhile, has reveled in casting himself as an outsider. And in solidly Republican Alabama, he is a prohibitive favorite to win the Dec. 12 special election against Democrat Doug Jones.

Trump carried the Cotton State overwhelmingly in last year’s presidential election, but Strange’s rebuke casts doubt on his clout just eight months into his presidency ― which could signal troubles ahead for his party in next year’s midterm elections.

BRENDAN SMIALOWSKI via Getty Images President Donald Trump prepares to embrace Sen. Luther Strange at a Sept. 22 rally in Huntsville, Alabama.

Trump himself, at last week’s rally, conceded he may have made a mistake in his pick and bemoaned that a Strange loss would be depicted as a “total embarrassment” for him. He also pledged to campaign for Moore in the event of a Strange loss.

Moore successfully turned the nomination race into a referendum on McConnell while casting himself as pro-Trump, despite not having the president’s backing. He effectively portrayed Strange’s association with McConnell as an example of the Washington “swamp” that Trump has frequently derided and that many of the president allies believe is thwarting his legislative priorities.

“President Trump’s being cut off in his office. He’s being redirected by people like McConnell who do not support his agenda ― who will not support his agenda in the future,” Moore said during a debate with Strange last Thursday.

Moore, 70, is a staunch conservative who has made a litany of contentious statements over the years. He became known nationally for his refusal to remove a monument of the Ten Commandments from the Alabama Judicial Building ― a stance that led to his ouster as chief justice of the state’s Supreme Court in 2003.

He won election to that office again in 2012, but was suspended from it last year after ordering state judges not to grant same-sex marriage licenses despite the U.S. Supreme Court’s nationwide legalization of such unions.

Moore has been particularly vocal on LGBTQ issues ― saying in 2005 that “homosexual conduct should be illegal.”

Bill Clark via Getty Images Roy Moore on the campaign trail in August. He overcame opposition from his party's national leaders to win the Republican Senate nomination in Alabama.

Strange, 64, had little success making a dent in the core of support for Moore, who appeared as impervious to attacks as Trump. Ads financed by the McConnell-linked super PAC disputed Moore’s conservative credentials. And on the eve of Tuesday’s vote, Strange upped the ante by comparing Moore to Todd Akin, the failed 2012 Senate GOP candidate in Missouri. Akin had been favored to win his race until he told an interviewer that pregnancy rarely occurs as a result of what he called “legitimate rape.”

In an interview Monday with The Washington Examiner, Strange said that ― like Akin ― Moore would prove “an anchor around the neck” of other Republicans because of his views.

Still, Moore led in every public poll preceding Tuesday’s vote.

Strange struggled to overcome his link to Robert Bentley, the widely disliked former Alabama governor who appointed him to the Senate in February. As Alabama’s attorney general, Strange faced accusations of stalling an investigation into Bentley, who resigned in April amid a sex scandal.

Tuesday’s vote was set up after Moore finished first and Strange second in a 10-candidate GOP primary last month. They faced off because neither received more than 50 percent of the vote in that first round of voting.

The runoff campaign spotlighted divisions in the Republican Party in the Trump era. Steve Bannon, who left his White House job as Trump’s chief strategist in mid-August and returned to his prior post of running the conservative website Breitbart News, marshaled anti-establishment forces ―including former Alaska Gov. Sarah Palin ― against Strange. And Bannon teamed up with Moore for a rally Monday night near Mobile, Alabama.

“We did not come here to defy Donald Trump, we came here to praise and honor him,” Bannon told the crowd. “A vote for Judge Roy Moore is a vote for Donald J. Trump.”

Jones, the 63-year-old Democratic nominee, has an impressive record. As a U.S. attorney in Alabama in 1997, he led the successful prosecution of two of the Ku Klux Klan members responsible for bombing Birmingham’s 16th Street Baptist Church on Sept. 15, 1963, in which four girls were killed. His campaign also got a boost last week when former Vice President Joe Biden announced he would stump for him.

So far, Jones has not faced much scrutiny. He “hasn’t had any single negative ad run against him yet,” Alabama radio talk show host Dale Jackson said. He added that Jones “was a defense attorney for a long time,” suggesting that the Democrat’s past caseload could become fodder during the general election campaign.

No Democrat has won a Senate election in Alabama since 1992. And that senator ― Richard Shelby, who remains in office ― switched to the GOP in 1994.

Democrats said they plan to tie other Republicans to Moore’s most controversial actions and statements, arguing that his candidacy will be a drag on the GOP across the country.

“Judge Roy Moore is unfit for office, and his primary victory means huge problems for Republicans across the country,” said Joshua Karp, a spokesman for Democratic opposition research group American Bridge. “Every Republican should have to answer for Moore’s extreme views and disgusting statements.”