Republican Jason Lewis narrowily defeated Democrat Angie Craig in a close race for the open seat in Minnesota’s Second District early Wednesday after hours of waiting for much delayed ballot returns from Dakota County.

Vote totals from the county that holds a significant percentage of the Second District’s population — including the cities of Burnsville, Lakeville, Apple Valley and Hastings — were delayed until after 2 a.m. Wednesday because of problems tallying the results from new voting equipment. With so many votes unreported for hours after the polls closed, neither candidate was ready to concede the race that had been one of the most closely watched in the state.

The battle for the seat held by retiring Republican Congressman John Kline drew attention — and a considerable amount of money — from both local and national political groups. Democrats took a particular interest in the race, as they looked at the swing district as a prime place to take back the seat that Kline has held since 2003.

A third candidate, Paula Overby of the Independence Party, also was on the ballot.

Craig, a former communications and human resources executive with St. Jude Medical, maintained a significant financial advantage throughout the campaign, raising more than $2.1 million between January and mid-October, a period in which Lewis, a syndicated talk-radio host, raised just under $685,000.

Both candidates were watching election returns come in with supporters at their respective parties’ gatherings in the Twin Cities — Craig with DFLers at the Minneapolis Hilton and Lewis with Republicans at the Radisson Blu at Mall of America.

In the days before the election, both of the major-party candidates crisscrossed the district, visiting businesses, colleges, homes and apartment complexes in communities stretching from Red Wing to Northfield, Burnsville, Eagan and Apple Valley.

Both said over the weekend that they were optimistic about their chances of victory, with both pointing to internal campaign polls that showed a competitive race.

The Second District is an increasingly purple spot on the election map, twice picking President Obama and backing Democratic Senate candidates in the last two elections — but electing only one Democrat to the U.S. House since the early 1940s.

As they had throughout the campaign, the candidates spent the final days of the campaign highlighting their sharp differences on issues ranging from immigration to climate change, but also echoed the critical tone that dominated much of both sides’ campaign advertising.

Frequent TV spots from Craig’s campaign and national Democratic groups aimed to compare Lewis’ brash on-air style to the comments of Republican presidential candidate Donald Trump, singling out comments he made about women, slavery and victims of Hurricane Katrina. Lewis said the comments were taken out of context.

His campaign took aim at Craig’s support for reforming, rather than repealing, the Affordable Care Act and her plans to spend on other programs.

Lewis championed a “flatter and fairer” tax code, across-the-board federal spending cuts, blocking Syrian refugees from entering the country and cutting back on environmental regulations, including the Clean Water Act.

He criticized state spending on light rail lines, favoring instead to put more money into highway construction, and said he aimed to undo the “mandates” of the Affordable Care Act. He also criticized Craig’s involvement with St. Jude’s political action committee and suggested she was involved in issues that led to lawsuits against the company.

Craig, meanwhile, disputed those claims, championing her work to hire veterans and her experience as a leader at a major company.

She said she’d focus on making college more affordable by expanding federal grants for low-income students, dedicate more funding toward the opioid addiction epidemic and invest in renewable energy — frequently noting that Lewis did not share her view that climate change is a real problem. Craig’s campaign, calling Lewis a “mini Trump,” suggested he did not have the temperament to serve an increasingly diverse Second District.

On Election Day, Craig rallied students at Carleton College and St. Olaf College in Northfield. On Facebook, she posted a photo of herself and her wife at their polling place, writing: “I never imagined I would mark the bubble next to my name on the ballot. This morning Cheryl and I went to the polls and didn’t just cast our vote for the candidates we believe in, we cast our vote for economic development, for high quality education, and for a fair shot for families across Minnesota’s Second District!”

Lewis spent part of his day visiting Burnsville High School. The night before the election, Lewis took to Facebook to urge his supporters to “cast a vote for freedom, prosperity, but most of all — truth. Yes, the uninterrupted stream of liberal lies, hatred and vitriol in the Second District is unprecedented,” he wrote.