One of the reddest states in the country just elected a Democrat in its governor race, rejecting a hardline anti-immigration activist whose rhetoric echoed and anticipated President Donald Trump.

Laura Kelly defeated Republican Kris Kobach and independent Greg Orman to win the governor race in Kansas on Tuesday night after a race that was often neck and neck between the two major-party candidates. Kobach, a polarizing, hard-right figure whose tenure as secretary of state was marked by legal battles over his voter registration laws, seemed to give Kelly a chance.

Related 4 winners and 2 losers from the 2018 midterm elections

Kelly also campaigned hard against the deeply unpopular outgoing Gov. Sam Brownback, who stepped down in January to become an ambassador; Brownback’s 2012 regime of severe tax and spending cuts more or less starved the government. (The legislature, in a bipartisan vote, reversed many of the tax cuts in 2017.)

Kansas has a well-earned reputation as a conservative state — it last voted for a Democrat for Senate in 1932, and for president in 1964 — but it has elected four Democratic governors in the past 50 years, most recently Kathleen Sebelius in 2006.

Still, throughout the campaign, a big question hung over the race: Would Orman, a business leader and frequent candidate running as an independent, play spoiler for Kelly, getting votes from moderate Kansas Republicans and independents turned off by Kobach but unwilling to vote for a Democrat? It was an outcome that even Orman’s former campaign treasurer — who resigned and endorsed Kelly just over a week before Election Day — seemed to fear.

In the end, though, Kelly prevailed.

She vowed to spend more on infrastructure and education. Perhaps one of her most consequential campaign promises, though, was to expand Medicaid — which would cover about 150,000 adults in the state, according to the Commonwealth Fund. She said she’d sign a bill expanding the program during her first year in office.

She relentlessly brought up Brownback and focused on public education, a key issue in the state. As the Democratic Party nationally got excited about new progressive voices, she ran as a pragmatic moderate. She racked up endorsements from the moderate Republicans who were once a powerful force in Kansas politics. Sebelius — the state’s former Democratic governor and President Obama’s first secretary of health and human services, tasked with enacting Obamacare — endorsed Kelly, but so did former Republican Gov. Bill Graves, Sebelius’s predecessor, and Nancy Kassebaum, Kansas’s Republican senator from 1978 until 1997.

Kobach, meanwhile, campaigned as a combative pro-Trump Republican, emphasizing his signature issues of immigration and voter fraud. He remained in office as secretary of state — the state official in charge of elections — throughout the campaign, as fallout continued from the controversial voter registration law that he pushed the state legislature to pass in 2013.

Kansas passed laws at Kobach’s urging requiring people not only to show photo ID at the polls but also to prove their citizenship when registering to vote. The law was thrown out in court earlier this year, and Kobach — a former law professor — was ordered to take a law class on evidence and rules of procedure.

The governor’s race is his latest defeat.