Democratic Senator Jon Tester campaigns on November 2, 2018, in Livingston, Montana.William Campbell / Corbis / Getty

Before he arrived at the balloon-decorated Holiday Inn in Great Falls, Montana, for his Election Night watch party, Jon Tester was 80 miles away on his farm in Big Sandy, taking the engine out of his ’86 Chevy pickup.

When the race was finally called on Wednesday morning, the engine still had a blown head gasket—but the 62-year-old farmer and two-term Democratic senator had managed to get his political career to turn over one more time.

The outcome had been far from certain. President Donald Trump, enraged that Tester had derailed his nomination of Ronny Jackson to lead the Department of Veterans Affairs earlier this year, had made unseating the senator a personal project. Polls showed Tester and his opponent, Republican State Auditor Matt Rosendale, running neck and neck in the lead-up to Election Day.

Voter registration in Montana was at a record high as people headed to the polls, and turnout has surpassed any midterm showing since at least 1994. Gallatin County, a Democratic stronghold in the mostly Republican state, was among the last to report results. The county clerk and recorder, Charlotte Mills, told Montana Public Radio in an interview nearly two hours after the polls had closed that the line of voters at the courthouse was still stretching out of the building. “It’s been out the door and up the block the entire day,” she said.

The last voter in line filed his ballot around 11 p.m. The state uses a paper system, and collecting results from far-flung precincts on a snowy, foggy evening contributed to the slow returns. A crash on Highway 191, the only direct thruway from West Yellowstone to the county election office in Bozeman, delayed the driver bringing stacks of ballots from the southern end of the county.

Tester’s victory, when it was announced at last, came despite Trump having visited the state four times in the past five months to rally for Rosendale. It was a remarkable focus on a state with just over 1 million people. When Donald Trump Jr. accompanied Rosendale on a two-day “Montana Victory Tour” bus trip in late October, they stopped in some towns where a non-negligible share of the population could fit inside Air Force One.

Trump won Montana by more than 20 points in the 2016 election, and he attempted to leverage partisan flash points—from Brett Kavanaugh’s Supreme Court confirmation to the migrant caravan—in his attacks against Tester.

“You need a man who’s going to vote for your agenda,” he told voters during his most recent rally in the state, just before bringing Rosendale onstage. “Your agenda is Make America Great Again.”

His strategy might have backfired.

Tester appears to have won with just over 50 percent of the vote—the first time he has ever captured a majority. He had the edge with women voters and took a whopping 67 percent of the youth vote, according to exit polls. And he won a higher share of independent voters than he did in 2012.

Tester has prevailed as a Democrat in a state that leans Republican largely on the strength of his local appeal: he flies back from Washington, D.C., to work on his farm nearly every weekend, and emphasizes the value of knowing your neighbors. Rosendale summed up his own platform as “Trump Conservative.” The race came down to partisanship versus place.

As Tester put it to me when I interviewed him earlier this year, riding through a wheat field in his combine: In Montana, a state with a pioneer history, “everybody’s got a little libertarian streak in them.”

That independence means a hard road to victory for a senator representing the supposed party of big government. But it also holds a lesson for Trump: Montanans like to make their own decisions.

We want to hear what you think about this article. Submit a letter to the editor or write to letters@theatlantic.com.