EAST HELENA, Mont. — After losing a close election last year with a campaign that barely mentioned the words “Donald Trump,” Greg Gianforte appears to have learned his lesson.

Possibly too well.

As the Republican candidate for governor, Gianforte, a billionaire technology entrepreneur from Bozeman, ran as far away as possible from Trump’s chaotic presidential bid. He declined to publicly endorse him after he was the party’s presumptive nominee. And he skipped the New York businessman’s lone visit to the state last May, issuing a press release that didn’t even use Trump’s name. (“Gianforte Welcomes Presidential Candidate to Montana,” the headline read.)

Though Gianforte later said he supported Trump as the GOP nominee, he rarely spoke of the former reality television star at his campaign events. And that support became even more tepid after a tape emerged of Trump making vulgar comments about women during a 2005 interview with “Access Hollywood.” Gianforte condemned Trump’s remarks and said he would only “reluctantly” continue to back the GOP nominee’s campaign because he didn’t want to see Hillary Clinton win the presidency. “This is all the more reason we need to have effective leadership at the state level,” Gianforte said at the time.

In November, Trump won Montana in a blowout, defeating Hillary Clinton by 20 points, and his victory had coattails. Republicans retained control of the state legislature and won every statewide office, except for one: by a margin of less than four points, Gianforte narrowly lost to Gov. Steve Bullock, the incumbent Democrat.

Many Republicans here still think Gianforte could have pulled off a party sweep had he cozied up to Trump a little more during the campaign. And Gianforte now evidently agrees. As a candidate in the special election to fill Montana’s single U.S. House seat, the former tech CEO is fully on the “Trump Train,” as he often puts it, presenting himself as an unwavering ally to the president — just in time to watch Trump’s poll numbers plummet, after months of scandals that threaten to cripple his agenda and undermine his political capital.

Greg Gianforte, right, with a supporter after winning the Republican nomination for Montana’s special election for U.S. House last March. (Photo: Matt Volz/AP)

The special election here, scheduled for May 25, could be a crucial first test for Republicans looking ahead to next year’s 2018 midterm elections on the tricky question of whether it is politically safer to stick with Trump or run against him. And it could help Democrats in red states, like Montana Sen. Jon Tester, who is up for reelection next year, learn how to best position themselves to win back working-class and rural Americans widely credited for Trump’s victory last year.

In television ads and on the campaign trail, Gianforte has fully assumed the Trump playbook, endorsing the president’s decision to fire James Comey, the former FBI director, and expressing skepticism about investigations into Russian attempts to influence the presidential election. Like many Republicans, Gianforte has revamped his public remarks to embrace familiar catchphrases of the Trump lexicon.

Speaking to about 100 supporters here in an old converted barn in the rolling countryside not far from the state capital on a recent Thursday morning, Gianforte sounded at times like he was playing Trump Mad Libs, mixing in his own words with the president’s slogans. “I want to help Donald Trump drain the swamp back in Washington,” he declared. “Let’s make America great again!”

But for Gianforte, the reshaping of his political persona has been more than just rhetoric.

On the campaign trail, he has presented himself in the mold of Trump — a wealthy businessman who didn’t need to seek public office but feels duty bound to give back to the country that has given him so much. Born in New Jersey, Gianforte moved to Montana two decades ago, where he founded a software company, RightNow Technologies, which he sold to Oracle in 2011 for $1.5 billion.

In recent weeks, he has received significant help in delivering that message. Though he has been criticized for holding fewer public campaign events than his rival, Democrat Rob Quist, when Gianforte has been out in public, he’s been joined by VIPs like Vice President Mike Pence and Donald Trump Jr., the president’s oldest son, who has repeatedly likened Gianforte to his father.

“We need more people in D.C. to help my father. When I think of the similarities … he’s not someone like so much of D.C. They need that job. He doesn’t need this,” Trump Jr. told supporters during a joint appearance with Gianforte in East Helena. “He built an incredible business that employed hundreds of people. … That’s like my father.”

Speaking to a crowd where many in attendance wore the “Make America Great Again” hat that became ubiquitous during last year’s campaign, Trump’s son pleaded with those who backed his father to turn out for Gianforte. He acknowledged the election could be viewed as a referendum on his father’s popularity. “Help him help my father,” he said.

This was widely regarded as a safely red seat. It had long been held by Republicans, most recently Ryan Zinke, who resigned in January after Trump appointed him interior secretary. But the race to replace him has become a focal point for liberal activists looking to score at least a symbolic victory over Trump.

Working in the Dems’ favor is an independent streak that has led Montanans to elect conservative Democrats like Bullock, Tester and former Sen. Max Baucus. And whether it’s that or the belief that Democrats have been newly energized to turn out against Trump, private polling on both sides suggests that Quist has cut Gianforte’s lead in the race to single digits in recent weeks. That’s in spite of massive spending by outside GOP groups and by Gianforte himself, who recently loaned his campaign $1 million — money that has been spent mostly on TV ads attacking Quist as a stooge of House Democratic leader Nancy Pelosi.

But Quist has also stepped gingerly around Trump in the race, which has likely benefitted his campaign even if it is at odds with Democrats who are contributing heavily to his campaign and surrogates like Vermont Sen. Bernie Sanders, who is set to campaign with the candidate in the state this weekend.

A political novice who has never run for public office, Quist is a singer-songwriter who works the trail with a folksy charisma, and dresses the part too. At a recent stop in Great Falls, he wore his ubiquitous white Stetson cowboy hat (which he kept on during his one and only debate with Gianforte), a Western-style shirt and distressed Wranglers and boots. He spoke softly and earnestly, peppering his stump speech with stories about growing up on a ranch in a tiny town near the Canadian border. As he does at nearly every stop—he has held a public campaign event nearly every day in recent weeks—he spoke about his key agenda item: the need to preserve and protect public lands, a huge issue in Big Sky Montana.

After a few minutes, as he has at dozens of campaign stops all over the state in recent months, the self-described “cowboy poet” picked up his guitar and played a few numbers, including what he described as his campaign theme song, a ballad that rails against critics and bullies that he says are hurting hard-working Americans. “If you stand with me,” he sang, “then I will stand up for you.”

For Quist, who has cast himself as a populist outsider, the song is a clear dig at Gianforte, whom he has cast as a rich guy out of touch with everyday Montanans. It is also an implicit critique of Trump, and though Quist criticizes many of the president’s policies, including the effort to repeal Obamacare and anti-immigrant reforms, the Democrat rarely attacks Trump by name.

Speaking after a recent stop at a bar in Billings, where he recited a poem about Montana because a sore throat kept him from singing, Quist explained why he has mostly kept the president’s name out of the race.

“To me, it’s not about Donald Trump. This is about Montana … and the things that are important to Montana,” Quist said, insisting that he didn’t want to “nationalize” the race.”

But he also emphasized he was also trying to win over conservatives and libertarians who had been swayed by Trump’s populist message in November. And echoing what Tester has said in recent months, Quist promised not to automatically oppose Trump, but to try to find common ground with the administration if he could.

“We need to focus on the things that we have in common,” he said. “If he truly is for getting dark money out of politics, if he truly is for that, I am for that too. And if he’s for infrastructure [spending] and manufacturing, that makes a lot of sense for the people of Montana.”

And it’s that centrist appeal that has many Republicans nervous heading into the May 25 election. Though Gianforte and his allies have blanketed Montana airwaves with ads attacking Quist, including for his personal finances and for having once performed at a nudist resort, the race is closer than anyone imagined.

Speaking ahead of a Gianforte rally here, a local Republican activist complained that Montana voters were being fooled by Quist, whom she derided as a “showboat entertainer with no substance” and who seemed to be “Teflon” against attacks. Reminded that her complaints sounded similar to those lobbed by Democrats against Trump last year, she rolled her eyes. “It’s not the same,” she insisted.

On stage, Gianforte attacked Quist repeatedly as someone who was hiding his more liberal beliefs, would take away guns and turn Montana into a “sanctuary” state for illegal immigrants. “Rob Quist is Nancy Pelosi in a cowboy hat,” he declared.

Working the rope line with Trump’s son nearby, Gianforte continued to press his case as he pressed the flesh. The choice, he said, using his trademark metaphor of a “Trump Train,” was this: “Do we put the brakes on the Trump Train or do we pick up speed?”