Even if Democrat Mark Herring ends up with more votes than his Republican rival Mark Obenshain in the tightly contested Virginia attorney general’s race, he could still lose.

Herring is currently ahead of Obenshain by a follicle–the current official count states that Herring has 164 more votes than Obenshain out of more than two million cast. A recount is all but guaranteed and litigation seems likely. But even if after the dust clears Herring remains in the lead, under Virginia law, Obenshain could contest the result in the Republican dominated Virginia legislature, which could declare Obenshain the winner or declare the office vacant and order a new election.

“If they can find a hook to demonstrate some sort of irregularity, then there’s nothing to prevent them from saying our guy wins,” says Joshua Douglas, an election law expert and professor at the University of Kentucky College of Law. “There’s no rules here, besides outside political forces and public scrutiny.”

An election contest is a specific post-election procedure for disputing the official outcome of an election. Different states have different rules for election contests–some put them in the hands of the courts, others in the hands of the legislature. Obenshain couldn’t simply contest the election out of the blue. He’d have to argue that some sort of irregularity affected the result. Still, Virginia law is relatively vague in explaining what would justify an election contest, and historical precedent suggests that co-partisans in the legislature are unlikely to reach a decision that hurts their candidate.

“History shows that contests in the legislature are generally more politicized than if they’re adjudicated in the judiciary,” says Edward Foley, a professor at Moritz College of Law. That applies to both parties–but it’s Republicans who have the majority in the Virginia General Assembly. The Virginia state senate is evenly split, but Republicans have the majority in the state house. A spokesperson for Obenshain didn’t respond when asked directly if, after exhausting all other avenues, Obenshain would pursue an election contest.

The Virginia attorney general’s race already has a potential justification–the controversy over nearly 500 provisional ballots in Fairfax County. Voters cast provisional ballots if there’s some question about their eligibility to vote. The idea is to ensure that every legitimate vote is counted, rather than turning voters away if say, they are mistakenly not on the rolls. Elections rarely come down to provisional ballots, but they’re also rarely this close. Both candidates announced Wednesday that they were moving ahead with appointing transition teams.

On Sunday the state board of elections informed the Fairfax County board that they had to observe state rules barring legal representatives of voters who cast provisional ballots from appearing on their behalf unless the voter is there in person. Whether that was a rule change or a clarification of existing practice is disputed–but the Fairfax County board also allowed voters who cast provisional ballots more time to appear in person and argue their eligibility than other counties. The county ultimately approved 271 provisional ballots–160 for Herring and 103 for Obenshain.

That discrepancy could be the basis for a court challenge, because legal experts believe the Supreme Court’s 2000 decision in Bush v. Gore requires jurisdictions within a state have to have uniform rules for counting votes. But if even if Obenshain loses in court, he could turn to the Virginia legislature with an election contest. The law states that Obenshain needn’t prove that Fairfax’s decision to allow more time for voters to argue their eligibility in order to go forward with an election contest, he need only “specific allegations which, if proven true, would have a probable impact on the outcome of the election.”

Obenshain may not have to prove that an irregularity would definitely have swung the election. “Virginia law permits a contest where a candidate can show a ‘probable impact’ on the result of an election, which is a relatively lax standard compared to other states,” says Foley.

Nevertheless there is a substantial political risks to this approach. Virginia Republicans could incur a severe political backlash if Virginians see them as thwarting the will of the electorate or subverting the results of an election simply because they didn’t like the outcome. Between a recount and potential litigation, we’re also still a long way away from a potential election contest.

Yet a campaign to persuade voters that the close election was simply the result of Democratic shenanigans at the polls isn’t inconceivable, especially with a base convinced that in-person voter fraud, which is very rare, is a deciding factor in elections. If after all options have been exhausted, Obenshain decides he wants to take his case to the state legislature, the only thing stopping Republicans from ordering a new election or declaring him the winner would be fear of a political backlash or their own self-restraint.

“It’s absurd to have a partisan legislature have the final say in who wins a close election, particularly with so few standard to guide it,” says Douglas. But it could happen.