Katie McGinty won the hotly contested Pennsylvania Democratic primary Tuesday, setting up a critical race against Republican Sen. Pat Toomey this fall that will help determine the Senate majority next year.

A relative newcomer to elective politics, McGinty used the endorsement of President Obama and more than $4 million in spending on her behalf from outside liberal groups to knock off former congressman Joe Sestak, whose solo style of campaigning sparked a bitter feud with Democrats in Washington. After a truce effort failed last year, Senate Democrats worked to find someone other than the former Navy admiral to run against Toomey, believing Sestak’s quirky personality would hurt them in such a key contest.

With 64 percent of precincts reporting late Tuesday night, McGinty led Sestak, 42 percent to 30 percent, according to the Associated Press, calling the race in her favor. John Fetterman, mayor of Braddock, a small town outside Pittsburgh who ran as an acolyte to Sen. Bernie Sanders’ presidential campaign, held 21 percent.

In other congressional news, Philadelphia Rep. Chaka Fattah (D), a 22-year veteran of the House, lost to state Rep. Dwight Evans (D), becoming the first congressional incumbent to fall this primary season. Fattah is under federal indictment related to how he financed his unsuccessful 2007 mayoral campaign. His trial could start later this year.

McGinty, a top environmental adviser to Bill Clinton’s White House, is just the third female nominated by either party in Pennsylvania to run for either Senate or governor in the past 30 years; no woman has ever won either high office in the Keystone State.

Looking to the general election, Senate Democrats made the calculated bet that Pennsylvania voters would want to double-down on history by trying to elect Hillary Clinton as president and McGinty as their first female senator. But in the process Democrats ran a sometimes brutal campaign that alienated some local activists who felt that Washington leaders played a bigger role in the nomination than did local voters.

Senate Democrats are well aware that they must heal the wounds from the Democratic race to unify ranks as they prepare for the fall election against Toomey, who has already stockpiled $10 million for the general election.

“I think it’s about communicating and it’s about respect. Look, people do things in the battle of war, that’s part of the deal. And campaigns are war, so to speak,” Sen. Jon Tester (Mont.), the chairman of the Democratic Senatorial Campaign Committee, said Tuesday before the race was called.

Toomey is one of six Republican incumbents up for re-election in November in a state that President Obama won in 2008 and 2012, which along with the open seat in Florida — which the president won twice — gives Democrats ample targets in their effort to win back the majority. With 46 Senate seats now, Democrats need a four-seat gain if their nominee wins the presidency, in which the vice president would break the tie in their favor, or a five-seat gain to claim the outright majority.

Most recently, McGinty served as chief of staff to Gov. Tom Wolf in Harrisburg, before entering the Senate race late last year. She previously served as then-Gov. Ed Rendell’s secretary of Environmental Protection. Her only previous race, against Wolf and several other Democrats in the 2014 bid for the gubernatorial nomination, ended with a distant fourth place finish.

Sestak tried to tap into the anti-establishment vein that is running through both political parties, touting his fights with Democrats in Washington as a badge of honor. He tried to model his race on his 2010 upset over Arlen Specter, the longtime Republican senator who switched parties in 2009 with the backing of Obama, Vice President Biden, Rendell and other party elders.

This time Sestak was not the only anti-establishment candidate. Fetterman — a bald, hulking, tattooed mayor who openly embraced Sanders and his insurgent campaign against Clinton — was also in the contest. A fourth candidate, Joseph Vodvarka, who only won his spot on the ballot a week ago, also ate into votes that might have gone to Sestak, particularly in Allegheny County, home to Pittsburgh.

One other Pennsylvania incumbent, Rep. Bill Shuster (R), faced an intense fight to retain his House seat. After trailing much of the night, the chairman of the powerful Transportation and Infrastructure Committee defeated local businessman Art Halvorson (R), 52 percent to 48 percent, with more than 80 percent of precincts counted.