Republicans were clinging to their Senate majority on Wednesday, as they successfully fended off Democratic challengers in key battleground states and held narrow leads in other critical races despite a difficult political environment for the GOP.

For much of this year, Republicans contended with an unpopular president, a nation grappling with a public health crisis and a slate of energetic Democratic challengers in states from Colorado to Arizona to Maine.

That mix posed the most significant threat to Senate Majority Leader Mitch McConnell’s 53-to-47 majority since the GOP took control of the chamber in 2014, jeopardizing what had become a key bastion of Republican power.

But Republicans defeated well-funded Democratic challengers in South Carolina, Iowa and Montana while seizing a lead in the key battleground of North Carolina. Democrats defeated Republican Sens. Cory Gardner in Colorado and Martha McSally in Arizona, although the GOP picked up a seat of its own in Alabama and appeared to diminish the prospects of a Democratic majority as results continued to roll in Wednesday morning.

“All I can say is, this has been overwhelming,” Sen. Lindsey O. Graham (R-S.C.) said after defeating Democrat Jaime Harrison, whose campaign broke fundraising records and brought a hard-fought Senate race to the deeply conservative state. “I’ve never been challenged like this, and I’ve never seen more support before than tonight.”

Saying he would “be there for the conservative cause,” Graham, a perennial dealmaker before he fashioned himself into a Trump ally, added: “To my Democratic friends and opponents — if you want to meet in the middle, I’ll be there to meet you. We’ve got a great country. Let’s work together to make sure it stays great.”

Republican leaders contended that they had made up ground in recent days and that many GOP senators had stabilized their campaigns. McConnell (R-Ky.) told reporters last week that holding onto the Senate was a “50-50 proposition.” He coasted to victory in his own bid to secure a seventh term.

“We know our next president will need to unite the country, even as we all continue to bring different ideas and commitments to the table,” McConnell told reporters at a Louisville hotel Tuesday evening, before the more competitive Senate races were called.

Early Wednesday, Democrats had claimed two GOP-held seats, as former Colorado governor John Hickenlooper defeated Gardner and Democrat Mark Kelly unseated McSally in Arizona. One of those losses was offset by Republican Tommy Tuberville’s win over Sen. Doug Jones (D) in Alabama.

In a brief address on Facebook Live, Hickenlooper said record voter turnout in Colorado sent a message that voters were fed up with the bitter polarization in Washington.

“It’s time to put the poisonous politics of this era behind us,” he said. “Clearly people are saying it’s time to turn the page.”

But in North Carolina, Sen. Thom Tillis (R) had a narrow 48.7 percent to 46.9 percent lead over Democrat Cal Cunningham with about 98 percent of votes counted. Democrats had high hopes of knocking off Tillis en route to a majority, although late-breaking revelations of an extramarital affair on Cunningham’s part dogged the Democrat in the final weeks of the campaign.

Speaking to supporters in Mooresville, N.C., Tillis said he had carried a heavy burden: saving the majority for Republicans in the Senate.

“We continue to deliver results because delivering results still matters,” Tillis said. “Tonight, with six more years in the U.S. Senate, I will continue . . . to make this nation and this state as great as it can possibly be.”

Two more remote Democratic targets, Sen. John Cornyn (R-Tex.) and Republican Roger Marshall in Kansas, were also on track to win and keep those states out of Democratic hands.

Heading into Election Day, Democrats believed they had an edge over Gardner, Tillis, McSally and Sen. Susan Collins of Maine. Democrats needed to gain four seats for a majority, or three seats for a 50-50 tie in the Senate, which would give them control of the chamber if Joe Biden won the presidency.

The battle for the Senate has been ferocious for months because control of the chamber will be crucial in determining the success of Biden’s first term or Trump’s second. With the House likely to remain in Democratic hands, a Democratic Senate could smooth the way for Biden’s agenda to advance in Washington, while a GOP-led Senate could do a great deal to block it.

As for Trump, among his biggest successes has been the confirmation of dozens of young conservative judges, capped by the elevation of Judge Amy Coney Barrett to the Supreme Court last week. If Trump is reelected and the Republicans hold the Senate, they could continue to tilt the federal judiciary to the right, potentially for a generation.

The Senate outcome was not immediately clear, with slower-than-usual counting in several states because of the massive volume of early balloting and the unique voting systems in Maine and Georgia, which hosted competitive Senate races.

Republican senators headed into the election cycle on relatively favorable terrain, with just two GOP senators up for reelection in states that Trump lost in 2016. Republicans were boosted by a strong economy and a battle-tested freshman class that had been catapulted into Washington on a red wave six years ago.

But far more Republican than Democratic senators were facing reelection this year, and many of them had difficulty distancing themselves from an erratic president who disrupted democratic norms, challenged traditional GOP orthodoxy and struggled to manage a pandemic that has left more than 232,000 Americans dead.

Republican senators have taken a particular hit on health care, as they sought to reconcile their failed crusade to repeal the Affordable Care Act with a pledge to protect coverage for people with preexisting conditions. Polls regularly show that health care is among the issues voters care most about, and the coronavirus pandemic has only underlined the issue’s importance.

Republicans have sought to counteract these trends by touting their accomplishments achieved in tandem with the White House, such as a sweeping 2017 tax-cut law and the confirmation of three Supreme Court justices. They have also argued that they are best qualified to restore the booming U.S. economy that was cut short by the pandemic.

But by tying themselves so closely to the president, GOP senators ensured that Trump’s struggles in many cases became those of the Republican majority, and GOP officials working on Senate races began openly blaming the president’s sinking approval ratings as their majority began to look more tenuous in recent months.

Like Trump, Senate Republicans were not able to notably expand their electoral map, and they largely remained on defense. The biggest exception was Republican challenger John James in Michigan, who has fought a close race with Democratic Sen. Gary Peters. That contest had yet to be called early Wednesday.

In contrast, Democrats steadily expanded their offensive opportunities, forcing competitive races in conservative states such as South Carolina, Kansas and Alaska and sending Republican operatives racing to rescue GOP senators and candidates in those states.

While Republicans were likely to hang on to at least some of those states, the party was forced to spend money and resources it would have preferred to use elsewhere.

In all, 23 Republican-held seats were on the ballot this year, compared with just 12 held by Democrats, creating an inherently dangerous playing field for Republicans.

Of those dozen Democratic seats, only two — Alabama and Michigan — were considered remotely vulnerable. Meanwhile, Democrats mounted strong challenges in about a dozen Republican-held seats, from bluish-purple Colorado to the GOP stronghold of Texas.

The challengers recruited by Democratic leaders included current and former Democratic governors, a former NASA astronaut, and the speaker of the Maine House of Representatives. The Democrats posted massive fundraising figures that shattered records from previous cycles, capitalizing on grass-roots enthusiasm from rank-and-file Democrats motivated to sweep Republicans at all levels out of office.

In conservative South Carolina, Harrison posted the highest single-quarter fundraising total by a Senate candidate in U.S. history, raising $57 million in the third quarter of this year in his bid against Graham. Graham has earned particular scorn from many Democrats for his transformation from Trump critic to Trump acolyte, as well as for his role in pushing through Barrett’s confirmation after blocking President Barack Obama’s last Supreme Court nominee.

For his part, a stoic Harrison recited the South Carolina state motto, “While I breathe, I hope.”

“You know our state motto rings true, even on a day like today,” Harrison told supporters in Columbia. “You know, we did something incredible here in South Carolina. We proved that public office is not a lifetime job and that people are willing to hold our leaders accountable.”

The Democratic Senatorial Campaign Committee (DSCC), the official campaign arm of Senate Democrats, said its candidates in more than a dozen of the most competitive races had, as a group, raised more than double their Republican opponents in the first two weeks of October, which encompass the final pre-election fundraising reports.

Republicans had long braced for a loss in Colorado, where Gardner did not overcome the increasingly Democratic tilt of the state, while Democrats never thought they would retain Alabama, where Jones in 2017 saw a rare Democratic victory in the state over Roy Moore, a deeply flawed Republican candidate.

Democrats were also hoping for a victory in Maine. Collins, one of the few remaining moderate, pro-abortion rights Republicans who has exerted her centrist influence in the Senate for years, was fighting her most competitive reelection race ever against Democrat Sara Gideon, the speaker of Maine’s House.

Polling has shown that Maine’s unique ranked-choice system, which allows voters to rank candidates by preference, may ultimately help Gideon because more people who favored third-party hopefuls were likely to choose her as their second choice. But as of early Wednesday, Collins was hovering just under 50 percent — the threshold at which she would win outright, without the ranked choice system kicking in — with 69 percent of the votes counted.

The Senate races in North Carolina and Iowa, respectively, turned out to be the most and second-most expensive races in U.S. history, another reflection of how this election cycle has generated more passion and activism from voters than any in recent memory.

In Iowa, Republican Sen. Joni Ernst defeated Democrat Theresa Greenfield in a contest that Republicans had viewed as part of a firewall against a Democratic majority, given Iowa’s decisive vote for Trump in 2016.

“I will spend the next six years to live up to the honor you have given me,” Ernst said in her victory speech, with a voice made hoarse by weeks of furious campaigning. “This has been a difficult year and a challenging election for all of us. It is time to start healing — let’s turn down the rhetoric and start listening again.”

In Montana, popular Gov. Steve Bullock — recruited into the contest by Senate Minority Leader Charles E. Schumer (D-N.Y.) after Bullock’s failed presidential bid — lost against Sen. Steve Daines (R), further shoring up that GOP firewall.

Still being watched was Alaska, where Trump’s sinking approval ratings forced Republicans to expend resources to protect Sen. Dan Sullivan against Al Gross, an independent backed by the Democratic Party.

Given the closeness of the fight for Senate control, the two races in Georgia were attracting extra attention, since they would go into a runoff if no candidate initially earned 50 percent of the vote.

The special election to finish the term vacated by former senator Johnny Isakson (R-Ga.) is officially headed into a Jan. 5 runoff, with most Democrats consolidating behind Raphael Warnock, the pastor at Atlanta’s Ebenezer Baptist Church, while appointed Sen. Kelly Loeffler was ranked in second after Tuesday’s vote. Rep. Douglas A. Collins (R-Ga.) who had waged what turned out to be a nasty intraparty brawl with Loeffler, conceded to her earlier Tuesday night and endorsed her.

“Y’all know how important it is that we all come together,” Loeffler told supporters in Atlanta. “Because the radical left wants to take over this country. We’re going to fight back against that. I welcome the opportunity to take the lead and build strong allies across our state, because that’s what it’s going to take.”

It was less clear whether a runoff would be necessary in the race between Sen. David Perdue (R-Ga.) and Democratic challenger Jon Ossoff, who would be the youngest sitting senator if he won. Ossoff took advantage of the state’s changing demographics and a political environment favorable to Democrats to turn that contest into one of the most competitive nationwide.

Pam Kelley in Huntersville, N.C.; Haisten Willis in Atlanta; Chris Dixon in Columbia, S.C.; Mark Guarino in Des Moines; Jennifer Oldham in Denver; and Josh Wood in Louisville contributed to this report.