Democratic Sen. Jon Tester defeated Republican Matt Rosendale in Tuesday’s Montana U.S. Senate race despite a blizzard of attacks from Donald Trump and his allies ― an impressive feat in a state the president carried by 20 percentage points in the 2016 election.

Tester was one of the Democratic Party’s most vulnerable incumbents this year, but he surprised progressives with his frequent opposition to Trump’s executive and judicial branch nominees. He voted against the appointments of Health and Human Services Secretary Alex Azar and Secretary of State Mike Pompeo, for example. He opposed Gina Haspel, Trump’s controversial choice to lead the CIA. He also voted against both of Trump’s Supreme Court nominees, Neil Gorsuch and Brett Kavanaugh.

What Trump resented most, however, was the senator’s role in derailing the nomination of former White House physician Ronny Jackson to lead the Department of Veterans Affairs. Jackson withdrew his bid earlier this year over allegations that he oversaw a toxic work environment, overprescribed medication and drank while on the job. Tester, the senior Democrat on the Senate Veterans’ Affairs Committee, made enough noise about the allegations that even some top Republicans on the panel sided with him against the president.

William Campbell via Getty Images Jon Tester won re-election to his U.S. Senate seat in Montana.

“He behaved worse than the Democrat Mob did with Justice K!” Trump complained last month on Twitter, comparing Tester’s opposition to Jackson to the protests against Kavanaugh’s nomination.

Republicans hoped that Tester’s opposition to Jackson would hurt the senator’s standing in Montana, a state that has one of the highest rates per capita of military veterans in the United States. The strategy never quite took off, however. Tester’s success at building his own brand, and his efforts to portray himself as a bipartisan voice who isn’t afraid to work with the GOP, translated into a comfortable polling lead throughout much of the race.

Tester, a farmer who as a child lost three fingers on his left hand in a meat grinder accident, started in politics as a local school board member. After two terms as a state senator, in 2006 he won his U.S. Senate seat by knocking off a longtime GOP incumbent. Six years later, he won re-election by 4 percentage points as Republican Mitt Romney carried Montana in the 2012 presidential race by more than 13 points.

Rosendale, the Montana state auditor, sold himself as a political outsider in the mold of Trump. He attacked Tester over his votes against the president’s policies and nominees, but he suffered from attacks labeling him as a carpetbagger. Democrats dubbed him “Maryland Matt” over the fact that he was born and raised in Maryland, and they further questioned his credentials as a rancher.

Rosendale got a boost last week when the Libertarian Party candidate in the race, Rick Breckenridge, threw some of his support behind him. The announcement came too late, however, as Rosendale’s campaign struggled to keep up with Tester’s large fundraising advantage.