Democrats managed to avoid being shut out of the general election in several key congressional races in the California primary on Tuesday, as their candidates finished second to Republicans, allowing them to qualify for the November ballot — barely.

California has a “top two” or “jungle” primary, in which all candidates from all parties compete in a common pool, and the first and second place finishers advance to the general election, regardless of party. That means two members of the same party can theoretically advance to the November contest, shutting out the other party.

Democrats faced the prospects of a shutout in several Republican-held districts that voted for Hillary Clinton in 2016, simply due to a glut of candidates who split the vote between them. They faced a “disaster” or “nightmare” scenario in which they would not be able to compete for winnable congressional seats that they would need to capture if they hoped to take the 23 seats nationwide necessary to win the House majority and restore House Minority Leader Nancy Pelosi (D-CA) to the speaker’s chair.

After spending millions of extra dollars to avoid shutouts, Democrats managed to avoid elimination in the 39th, 48th, and 49th districts.

Gil Cisneros won the right to compete with Republican Young Kim for the open seat being vacated by retiring Rep. Ed Royce (R-CA) in the 39th. Democrats Harley Rouda and Hans Keirstead both edged Republican Scott Baugh in the 18th district, guaranteeing that one of them will face incumbent Republican Dana Rohrabacher in the fall — though the two of them were separated by fewer than 100 votes as of Wednesday morning. And Democrat Mike Levin will face off against Republican Diane Harkey for Rep. Darrell Issa’s seat in the 49th district.

That has Democrats nationwide breathing a sigh of relief. However, Democrats were also nearly shut out in other competitive districts where that had not seemed to be a real threat, including the 10th and the 50th. They were, in fact, shut out in California’s 8th district, where incumbent Republican Paul Cook will face conservative radio host (and former Breitbart News contributor) Tim Donnelly in November — though it was not seen as a likely Democrat pickup.

With Republican businessman John Cox capturing second place in the race for governor, Democrats will face an energized Republican base that will likely turn out statewide in November, boosting Republican congressional candidates.

The task of winning the U.S. House just became that much harder.

Joel B. Pollak is Senior Editor-at-Large at Breitbart News. He was named to Forward’s 50 “most influential” Jews in 2017. He is the co-author of How Trump Won: The Inside Story of a Revolution, which is available from Regnery. Follow him on Twitter at @joelpollak.

This post was updated to take CA-08 into account, as well as Mike Levin’s second-place finish in CA-49.