ANCHORAGE, Alaska — The Alaska Supreme Court on Wednesday reversed a lower court, and reinstated Dean Westlake as the winner of a disputed House election.

“I’ve been dancing in my office for the last hour,” said Westlake’s attorney, Thomas Amodio. “They got it right. Four of them got it right, anyway, but that’s all that matters.”

The court issued its two-page decision within five hours of hearing oral arguments in the case, with one judge partially dissenting.

The high court had hoped to reach a quick decision so ballots could be shipped to villages in the Montana-sized House District 40 beginning Monday.

Westlake had won the Democratic primary by eight votes, but the incumbent, Rep. Benjamin Nageak of Barrow, challenged. He had claimed errors by election workers in Shungnak and other places cost him the election.

Fifty voters in that village were given both Republican ballots and a ballot for all other parties, including Democrats.

The state had argued that while there were errors, they did not rise to the level of malconduct, which is the threshold in state law to overturn the election. The state had argued that the Democrats have an open primary, meaning anyone can vote regardless of party. Since there was no Republican in the race, no one who received the two ballots voted improperly because they were entitled to vote in the race.

Nageak challenged, and state Superior Court Judge Andrew Guidi last week ruled in his favor. Guidi reduced Westlake’s vote tally by 12 votes and Nageak’s by two. Then he determined Nageak won the election, and ordered the state to certify him the winner.

The state appealed, and the case was put on an accelerated track.

“The primary task of the Division of Elections is to run fair, impartial, and efficient elections in which every qualified voter is permitted to cast a ballot and know that their vote will count,” Elections Director Josie Bahnke said in a statement. “The Division is pleased that the Supreme Court agreed that the election result as certified by the Division should stand.”

The court did not issue a written opinion, saying that would come later. However, Justice Daniel Winfree did issue a one paragraph explanation in the opinion.

He agreed that the lower court’s order to certify Nageak the winner “was legal error and must be reversed.”

However, he did agree with Nageak that the Division of Elections committed malconduct that could have affected the outcome. “Accordingly, I would declared the election void and direct the Division of Elections to hold a new election,” he said in the partial dissent.

Attempts to reach Westlake weren’t immediately successful.

A message left Wednesday afternoon at Nageak’s home was not immediately returned. An after-hours email seeking comment also was sent to Nageak’s attorneys.

Even though the disputed election came in the Democratic primary, this race could help shape the makeup of the House. Nageak caucused with the majority Republican party in the House. He and another rural Democrat, Rep. Bob Herron of Bethel, were successfully targeted by the state Democratic party, which supported their Democratic challengers.

Westlake faces no challenger in the general election.