Before the Brexit vote, economists were near-unified in voicing fears that the UK will suffer outside the EU. The list of consequences is long: The UK’s economy will lose out on a favorable trade union, businesses may relocate their European headquarters, and the whole episode could very well spark a recession for the country.

People who were counting on reason to win out were left depressed and confused after the votes were tallied. What they missed was, fundamentally, an understanding of the psychology of voting. Given how unlikely it is that any one person’s vote will be the decisive one, voting decisions can be driven more by emotion than by rationality. And Brexit had a very powerful emotion on its side — fear of outsider and loss of identity.

People who think facts and figures should have swayed the Brexit vote aren’t considering the psychology of fear.

The Brexit vote is proof that when emotions battle reason in a voting booth, emotions can win. This fact stuns some pundits but it’s less surprising for neuroscientists like Mina Cikara.

"Fear is an extremely motivating emotion," Cikara, who studies group conflict at Harvard, tells me. "And [politicians] who want to motivate others to engage in collective action are very smart to wield it." Her work shows that this fear doesn’t have to be based on "real" threats. Just the perception of a threat will do.

In the case of the Brexit, the motivating fear was — in part — xenophobia.

European Union membership requires open borders. In the UK, the result of membership has been a massive influx of European immigrants over the last 20 years. And with the immigrants came anti-immigrant anxiety.

"The percentage of Britons ranking ‘immigration/race relations’ as among the country’s most important issues has gone from near zero percent to about 45 percent," Zack Beauchamp explained. "Seventy-seven percent of Brits today believe that immigration levels should be reduced."

(The anti-immigrant sentiment is itself somewhat irrational. The London School of Economics and Political Science points out "EU immigrants pay more in taxes than they take out in welfare and the use of public services.")

Once we’re fearful of outsiders, Cikara and other psychologists have shown, our whole worldview changes. We become more cliquish, we’re quick to dehumanize, we’re more gullible when it comes to fear-mongering rumors.

In recent years, politicians have gotten more effective at painting immigrants as dangerous outsiders. Look no further than Donald Trump. Or his UK counterpart, Nigel Farage, the politician who has stoked fears by asserting thing like Muslims "don’t want to become part of our culture."

It’s important to understand where our fear of outsiders come from. It's a deeply human emotion. And the more we understand it, hopefully, the more we can tune it out. That’s important in a world where cultural integration is more common than ever before in human history.

Here are some key lessons from the research.

Lesson 1: Fearing outsiders is one of our oldest psychological tendencies.

There’s a reason why every country with immigration has pockets of xenophobia. It’s our instinct to be distrustful of those who we perceive as being "them." In prehistoric times, this is what kept us safe. In the modern age, it’s what nudges us toward bigotry.

In lab experiments, it’s shockingly easy to pit people against one another. Many experiments start with randomly assigning people arbitrary teams.

"Once you trip this wire, this trigger, this cue, that you are a part of ‘us versus them,’ it’s almost like the whole brain become re-coordinated in how it views people," Jay Van Bavel, an NYU psychologist, told me in 2013. He often just uses "red" teams and "blue" teams for his experiments. Once on a team, participants immediately start showing bias toward their teammates. They’ll like their own teammates better. They’ll spend more time looking at their faces. They’re more likely to remember them.

Lesson 2: When we fear outsiders, we dehumanize them

You can think of human psychology as a series of evolutionarily coded computer programs. These programs tell us how to react to new situations. When we see a baby, we want to pinch its cheeks. When we see a threatening stranger, we either want to fight them or flee from them.

Fearing others changes the way we perceive them. In experiments, this plays out in very literal and disturbing ways: In some experiments, psychologists can get participants to rate outsiders as having fewer human qualities.

"It’s what we call an over-exclusion bias," Cikara tells me.

When you start fearing others, she says, "your circle of who you counted as friends is going to shrink. And that means those people outside of the bounds get less empathy, get fewer resources."

It also means you become more vigilant and obsessed with marking who is an insider and who is not. "You want to draw those boundaries brighter, so you don’t make any mistakes about who you want to share your resources with or who you want to trust," she says.

This fear can then be compounded by in-group pride. "People who empathize more with their own groups tend to be more aggressive toward the out group," she says.

Lesson 3: When we fear outsiders, our brains exaggerate their threat

New York University psychologist Jay Van Bavel and his colleague Y. Jenny Xiao illustrated this concept nicely in a 2012 paper.

The test was simple: The researchers asked participants to estimate the straight-line distance from New York to Mexico City. Participants who expressed more animosity toward Mexican immigrants rated Mexico City as being several hundred miles closer to New York than people who felt less threatened. And, Van Bavel adds, "We have new data showing that if people think the wall between the countries is secure, this effect goes away."

Lesson 4: Anecdotes that instill fear of outsiders are much, much sticker than facts and figures.

Economic forecasts don’t win ballot measures. Emotions do.

Our brains are built to be vigilant. We’re constantly on the lookout for threats. That’s why stories about immigrants committing crimes, stories about neighbors losing jobs to immigrants, and assertions that immigrants aren’t loyal to their adopted countries, are all extremely powerful (regardless if they are true).

"Once you can get that one story out there, it’s enough to start the cycle of people thinking this way and changing how people think about these out-groups," Cikara says. "People are very sensitive to anecdotes, more than they are to abstract representations of data."

Our minds have evolved to think in mental shortcuts — heuristics — but in the modern age they can lead us astray.

Lesson 5: The research is pretty pessimistic, but it also lends some hope

So our brains love seeing the world in terms of "us versus them," but there’s nothing in our brains that defines who "they" are. Experiments often put people into arbitrary teams. So do societies.

In 2013, Van Bavel told me about a trick that sounded so simple and hopeful. Sometimes he’ll switch a red-team participant to the blue team and vice versa. "We say, 'Listen, there's been a mistake, you're actually on the other team,' " he told me then. "And the moment we do, we completely reverse their empathy. Suddenly they care about everybody who is in their new in-group."

That might be simple in the lab, but in the real world, these feelings are more deeply entrenched. But even in the real world, experimenters are showing there are ways to reduce bias.

"You can fight anecdotes with anecdotes," she says.

In April, I reported on an experiment that showed with just the right dose of empathy, canvassers could change voters’ mind on transgender rights issues. "Two decades of opinion change took place during a 10-minute conversation, and it persisted for at least three months — that's a big effect," Josh Kalla, one of the co-authors, told me. All it took was a conversation where the voter was asked to put themselves in a transgender person’s shoes.

That type of outreach is difficult, but not impossible.

Overall, Cikara wants people to understand that even though the human brain has so many inbuilt systems for prejudice, we’re not doomed to succumb to them.

"These systems are flexible, they can turn on a dime," she says. "if that’s true, they can also improve on a dime." Trumpism or the Brexit are not "the ultimate manifestation of something that evolution has programmed us to do," she says. "The machinery is there but it doesn’t have to go that way."

Correction: This post initially stated Nigel Farage is a member of the UK Parliament. He is not.