Despite well-justified fears of disaster, Democrats emerged from California’s primaries on track to field a candidate in every one of the state’s key House races this fall, ensuring Republicans will have to play defense in seven congressional districts that Hillary Clinton carried in 2016—and keeping Democrats’ path back to the majority wide open.

But it was a close and difficult thing. Thanks to a ballot measure that passed in 2010, California has forbidden political parties from nominating candidates in traditional primaries, a practice followed in almost every state in the nation. Instead, all candidates from all parties must run together on a single primary ballot, with the two highest vote-getters advancing to the November general election.

And here’s the real problem that makes this system so undemocratic: The top two finishers can both be from the same party. That means it’s eminently possible for two Republicans or two Democrats to wind up in the general election in any number of races, depriving voters of a genuine choice. (Voters aren’t even permitted to cast write-in ballots.) And because California’s primary electorate leans to the right compared to the state as a whole, competitive districts are far more likely to result in a Democratic shutout than the reverse, something that’s happened several times in the past.

Fortunately, that didn’t happen on Tuesday, but only thanks to a lot of hard work—and a lot of money.