WASHINGTON ― Sen. Pat Toomey (R-Pa.) eked out a re-election victory on Tuesday night, bringing an end to one of most closely watched ― and most expensive ― Senate races in the country.

Toomey defeated Democratic challenger Katie McGinty, 49 percent to 47 percent. She who had been leading slightly in the polls for months, but in the end she wasn’t able to seal the deal.

Part of Toomey’s strategy involved distancing himself from GOP presidential nominee Donald Trump. He dodged questions for months about whether he planned to vote for his party’s nominee, and didn’t reveal that he voted for Trump until about an hour before the polls closed. That may or may not have been necessary.

Toomey also tried, in the final days of his campaign, to align himself with President Barack Obama. He released an ad that featured a 2013 clip of Obama praising Toomey for his leadership on a bipartisan gun safety bill. The president issued a rare statement in response, dismissing the idea that he and Toomey share priorities.

“Pat Toomey may have done the right thing on one vote, but courage is telling Pennsylvania voters where you stand on the tough issues, not just the easy ones like background checks,” Obama said. “Voting to shut down the government and against bills to close the terrorist gun loophole isn’t courage. And playing politics with the Supreme Court isn’t courage.”

Toomey’s win, which wasn’t called until 1:18 a.m. on Wednesday, ensures that Republicans hold onto their majority in the Senate. It also caps off the most expensive Senate election in U.S. history. All told, outside groups and candidates spent more than $152 million in the race, according to the Center for Responsive Politics.