Rep. David Valadao, R-Calif., was on the brink of defeat Monday, the victim of smart political maneuvering by the Democratic Congressional Campaign Committee and President Trump’s relentless effort to curtail legal immigration.

Three weeks after the midterm elections, ballots were still being counted in California’s 21st Congressional District. Republicans monitoring the race predicted that the next batch would erase Valadao’s diminishing lead and put businessman T.J. Cox, the Democrat, on top.

Valadao is an outspoken proponent of immigration reform and protecting the so-called Dreamers, individuals brought to the U.S. illegally as young children by their parents. But the DCCC’s sustained, albeit quiet, investment in Cox, combined with Trump’s dire warnings about the dangers of immigration, are combining to oust Valadao.

“The president’s last-minute focus on chain migration probably really hurt here,” a Republican operative told the Washington Examiner in an email. “Can’t really point to anything specific the Valadao team did wrong — they ran a near perfect race but probably weren’t able to keep the focus local at the end.”

Valadao was considered among the safest bets for re-election at the outset of the 2018 election cycle. Even amid a blue tide that would end up sweeping House Republicans from power on Nov. 6, Valadao’s strong connection to the voters of his heavily Hispanic, Central Valley seat was thought enough to win him another term.

Indeed, Valadao easily won re-election two years earlier despite similar immigration rhetoric from Trump and the 21st District voting overwhelmingly for Democrat Hillary Clinton. The difference in 2018, the DCCC believes, is the work the committee did on the ground to capitalize on what turned out to be an electoral gold rush for the Democrats up and down California.

The DCCC spent $1.21 million. The money covered two field offices to drive voter turnout, two field organizers dedicated to Hispanic turnout, and a round of hybrid advertising that allows for a certain level of coordination between candidates and congressional committees. (The National Republican Congressional Committee also used hybrid ads in various races.)

“We knew Valadao could be beat,” a Democratic operative said in an email exchange, adding: “I definitely don’t think you can put this all on Trump. Valadao voted for repeal and the tax bill. That’s just insane. INSANE…The reality is he never moderated and it cost him dearly.”

The next dump of counted votes could come on Tuesday. Valadao’s loss would be the Democrats’ 40th House pick-up, capping what turned out to be their most successful midterm election since the Watergate era.