Of the 58 presidential elections in the history of the United States, 53 of the winners took both the Electoral College and the popular vote. But in five incredibly close elections—including those for two of the past three presidents—the winner of the Electoral College was in fact the loser of the popular vote.

Here's how that can happen: The U.S. president and vice president aren’t elected by direct popular vote. Instead, Article II, section I of the Constitution provides for the indirect election of the nation’s highest offices by a group of state-appointed “electors.” Collectively, this group is known as the Electoral College.

To win a modern presidential election, a candidate needs to capture 270 of the 538 total electoral votes. States are allotted electoral votes based on the number of representatives they have in the House plus their two senators. Electors are apportioned according to the population of each state, but even the least populous states are constitutionally guaranteed a minimum of three electors (one representative and two senators).

This guaranteed minimum means that states with smaller populations end up having greater representation in the Electoral College per capita. Wyoming, for example, has one House representative for all of its roughly 570,000 residents. California, a much more populous state, has 53 representatives in the House, but each of those congressmen and women represent more than 700,000 Californians.

Since most states (48 plus Washington, D.C.) award all of their electoral votes to the person who wins the statewide popular vote, it’s mathematically possible to win more electoral votes while still losing the popular vote. For example, if one candidate wins by large percentages in a handful of very populous states, for example, they’ll probably win the popular vote. But if their opponent wins a bunch of smaller states by tight margins, he or she could still win the Electoral College. That’s basically what happened in 2016.

Take a look at all five times a president won the White House while losing the popular vote.

John Quincy Adams (1824)

John Quincy Adams
History Archive/Universal Images Group/Getty Images
John Quincy Adams, 6th president of the United States.

This is the first of two occasions when the man ultimately elected president first lost both the popular vote and the electoral vote.

Back in 1824, there were four contenders for the presidency, all members of the same Democratic-Republican party: Andrew Jackson, John Quincy Adams, William Crawford and Henry Clay.

When the votes were tallied, Andrew Jackson won a plurality of both the popular vote and the Electoral College. (Although the popular vote was not measured nationwide at the time. Electors were chosen by popular vote in 18 states, while in six states, legislatures selected electors.) But to win the presidency, you need more than a plurality (the most electoral votes), you need a majority (more than half), and Jackson was 32 electoral votes shy of the mark.

In cases where no presidential candidate wins a majority of electoral votes, the Constitution sends the vote to the House of Representatives. According to the 12th Amendment, the House can only vote on the top three vote-getters, which eliminated Clay from the running, but that didn’t stop Clay from allegedly wielding his influence as Speaker of the House.

The House voted to make Adams president, even though Jackson had beaten Adams by 99 electoral votes to 84. Adams turned around and appointed Clay as his Secretary of State, infuriating Jackson, who accused his opponents of stealing the election in a corrupt bargain.

“[T]he Judas of the West has closed the contract and will receive the thirty pieces of silver,” said Jackson. “Was there ever witnessed such a bare faced corruption in any country before?”

Rutherford B. Hayes (1876)

President Rutherford B. Hayes
Merritt/Library of Congress/Corbis/VCG/Getty Images
Rutherford B. Hayes served as the 19th President of the United States.

Similar to 1824, the election of 1876 wasn’t decided by the voters, but by Congress. This time, though, the Constitution didn’t have an answer to the electoral crisis at hand.

The race was an ugly one between Republican Rutherford B. Hayes and Democrat Samuel Tilden, and when the votes were counted, Tilden won 184 electoral votes, exactly one vote shy of the majority needed at the time to win the presidency. Hayes only won 165, but 20 more electoral votes were still in dispute.

The Republicans objected to the results from Florida, Louisiana and South Carolina, since both parties claimed their candidate had won the states. What now? The Constitution had a backup plan if no candidate won a majority of electoral votes, but there was no such process for resolving a dispute.

So Congress created a bipartisan Federal Electoral Commission composed of House representatives, senators and Supreme Court justices. The Commission voted to give all 20 disputed electoral votes to Hayes, who won the election by the thinnest of margins: 185 to 184.

Why did the Commission decide to hand the election to Hayes, who had lost both the popular and electoral vote? Most historians believe there was a deal brokered between the two parties. The Democrats, whose stronghold was the South, agreed to let Hayes be president in return for the Republicans promising to pull all federal troops from former Confederate states. That’s one of the main reasons why Reconstruction was abandoned in 1877.

Benjamin Harrison (1888)

President Benjamin Harrison
Smith Collection/Gado/Getty Images
A portrait of President Benjamin Harrison, 1888. Courtesy Library of Congress. 

The 1888 race between incumbent Democratic President Grover Cleveland and Republican challenger Benjamin Harrison was riddled with corruption. Both parties accused the other of paying citizens to vote for their candidate. So-called “floaters” were voters with no party loyalty who could be sold to the highest bidder.

In Indiana, a letter surfaced that allegedly showed Republicans plotting to buy up voters and to disrupt the opposition’s own bribery efforts. Meanwhile, Southern Democrats did everything in their power to suppress the Black vote, most of whom aligned with the Republicans, the “party of Lincoln.”

When the nasty race was finally over, Cleveland and the Democrats took the entire South while Republican Harrison won the North and West, including Harrison's home state of Indiana by a slim margin. By sweeping the South, Cleveland won the popular vote by more than 90,000 votes, but he still lost the electoral vote 233 to 168.

Four years later, Cleveland came back and beat Harrison, becoming the first and only U.S. president to serve two non-consecutive terms.

George W. Bush (2000)

For the next 112 years, election results were back to normal with the winner of the Electoral College also taking the popular vote. Then came the hotly contested presidential election of 2000 that made it all the way to the Supreme Court.

The candidates were Republican George W. Bush, son of the former president, and Democrat Al Gore, who served as vice president under President Bill Clinton. On election night, the results were too close to call in three states: Oregon, New Mexico and Florida. Gore ended up winning Oregon and New Mexico by the slimmest of margins (just 366 votes in New Mexico), which left Florida to decide the presidency.

The race in Florida was so close that state law required a recount. When Florida Secretary of State Katherine Harris certified Bush as the winner by 537 votes, Gore sued, arguing that not all ballots had been counted. There were still piles of punch cards that had been set aside because of voter errors resulting in anomalies called “hanging chads,” “pregnant chads” and “dimpled chads.”

The Florida Supreme Court sided with Gore, but Bush appealed to the U.S. Supreme Court, which ultimately voted 5 to 4 to reverse the Florida court’s decision and halt the recount. With Florida in hand, Bush won the Electoral College 271 to 266, while Gore ended up getting 500,000 more votes in the popular vote.

Donald Trump (2016)

In a surprise victory that defied most pre-election polling, outsider Republican candidate Donald Trump beat Democrat Hillary Clinton, wife of the former president, Bill Clinton, despite the fact that Hillary Clinton received 2.8 million more votes in the popular vote—the largest such disparity yet.

Clinton performed very well in big cities and populous states like California and New York, where she beat Trump by 30 percentage points and 22.5 percentage points respectively. But Trump saw narrow victories in battleground states like Wisconsin (0.8 percent), Pennsylvania (0.7 percent) and Michigan (0.2 percent).

In the end, Trump may have lost the popular vote by millions, but he won the Electoral College convincingly with 304 electoral votes to Clinton’s 227.