According to this week’s United Nations estimates, humans are roaming far and wide: In 2015, there were 244 million international migrants, up from 173 million in 2000.

People are increasingly likely to meet foreigners, in other words. Yet many people are still wary of them. About half of Americans say immigration should be decreased. In November, 54 percent told pollsters they oppose allowing refugees from the Middle East into the U.S. (The xenophobic sentiment is more potent in some corners than others: Last week, a 56-year-old flight attendant wearing a hijab was escorted out of a Donald Trump rally for standing up in silent protest.)

The situation is similar in parts of Europe, with several European leaders saying outright they won’t accommodate Muslim asylum seekers. Earlier this month, Hungarian Prime Minister Viktor Orban called for—you guessed it—a wall. Or, as he put it, “a European defense line.”

Recent incidents like the San Bernardino shootings and the Cologne sexual assaults have certainly contributed to these anxieties. But on a broader scale, the human desire to keep out “the other” might be partly explained by a vestigial psychological quirk that was once meant to help protect us from infection. The mechanism, called the behavioral immune system, tells us to avoid things that are unfamiliar because they might harbor harmful pathogens.

The phenomenon helps explain a whole host of subconscious reactions, like prejudice against people with facial birthmarks, an attraction to symmetrical faces, and, yes, strong anti-immigration views. In some cases, the behavioral immune system might have a point: The sight of pus and blood makes some people queasy because touching another person’s infected wound without gloves is dangerous. But the behavioral immune system, like an allergy, is so sensitive that it’s often wrong. It sometimes hurts us more than it helps.

In a new review paper published this week in Advances in Experimental Social Psychology, the psychologists Damian R. Murray, from Tulane University, and Mark Schaller, from the University of British Columbia, explain where these behavioral defenses against infection came from—and how they can deceive.

The actual immune system—the one that biologically fights infection—works best against the pathogens it’s accustomed to. When European settlers reached America, they promptly infected Native Americans with measles, whooping cough, smallpox, and a number of other diseases to which they had no immunity. Today, we might experience this when we travel to new, glamorous locations and spend many un-glamorous evenings perched on the toilet with traveler’s diarrhea.

In centuries past, then, it may have been healthy to tread carefully around foreigners. Not only might outsiders carry dangerous germs, they also might not be familiar with the kinds of local customs—food-preparation laws or sexual practices—that conferred some measure of protection against communicable diseases.

The trouble is, with modern medicine most people no longer have to worry when they come into casual contact with someone from a different country. (Some researchers believe, in fact, that our lives are now too sanitary.) Still, our hypervigilant behavioral immune systems scan the landscape for foes, be they real or fake, to keep away from our delicate bodies.

A mere encounter with someone or something unfamiliar might set off our sense of disgust, an especially hair-trigger response. Here, our behavioral immune systems are helped out by the more primitive regions of the brain. According to New York University psychologist David M. Amodio, two things happen when we’re under threat and encounter a person from outside our group. First, the amygdala reacts by freezing our behavior—we stop, we stare—and preparing the body for fight or flight. And second, if the person is disliked, they might elicit a response from the insula, the brain region that generates a sense of deep-in-the-gut revulsion.

This impulse doesn’t always work in our favor. Some studies suggest the reason why pregnant women develop morning sickness in their first trimester, when their immunity is lower, is because they are more wary of infection and therefore more revolted by everything. In one study, people were less willing to eat fudge that was molded into the shape of poop, even if they knew for a fact it was fudge. The risk of accidental exposure to something harmful, our bodies seem to think, far outweighs the pleasure of a turd-shaped treat.

Pregnant women in their first trimester, as it turns out, also exhibit more ethnocentric and xenophobic attitudes than those further along in their pregnancies. That’s a trend researchers have found across studies and across borders: The more vulnerable we feel to disease, the more likely we are to want to build a proverbial wall (and make Mexico pay for it!). One early study by Schaller and others found that people who are more worried about getting sick are more likely to associate foreign groups with danger and have more hostile attitudes toward foreign-immigrant groups. In another experiment, Canadians who were shown images of infectious diseases were less likely to support immigrants from exotic-seeming countries, such as Mongolia, than were those who saw images of other types of threats, like car accidents.

The findings have held up across cultures. Over the course of several studies, Murray and Schaller have found that countries that suffered a greater prevalence of diseases like malaria and leprosy were also more likely to be collectivist and conformist, as measured by things like personality variation among the country’s citizens and the number of left-handed people. (In some more traditional cultures, naturally left-handed kids are forced to train themselves to become right-handed.) The two researchers have also found that people in more disease-addled countries are less likely to be extroverted or open to new experiences. If people would just behave traditionally, act in unison, and eschew strangers, the behavioral immune system’s thinking seems to be, maybe they wouldn’t catch diphtheria.

Could a subconscious fear of disease be part of what’s prompting Trump supporters to cheer his plan to ban Muslims and Mexicans? Not all Republicans are Trump supporters, of course, and few would endorse those particular Trumpisms openly. (Nikki Haley, the South Carolina governor who gave the Republican response to the State of the Union address, urged listeners to resist “the siren call of the angriest voices.”)

But there is some evidence that the behavioral immune system amps up certain brands of conservatism. One meta-analysis of 24 studies found that the strength of one’s behavioral immune system, as measured by things like a fear of contamination or sensitivity to disgust, is positively correlated with social conservatism—including ethnocentrism. In a not-yet-published paper, Schaller found that people who performed more Internet searches about Ebola around the time of the 2014 outbreak were more likely to vote Republican in the midterm elections. This only happened in right-leaning states, which suggested to Schaller that disease outbreaks, beyond reinforcing preexisting suspicions, also spur people to stick with the mainstream.

There are, of course, critics of this disease theory of nativism. As Ethan Watters wrote in Pacific Standard, some anthropologists argue that the correlation and causation issues within these studies are still too messy. And openness to strangers might be better explained by other factors, like strong government institutions that reduce the reliance on family ties. Because what people consider “foreign” changes over time and across borders, it’s impossible to know whether any given xenophobic rant is a product of latent germophobia.

And although the behavioral immune system helps explain the emotional appeal of xenophobia, it’s not destiny. It affects Democrats, Republicans, Syrians, and Europeans equally, after all, and it gets filtered through context and cognition.

Even Schaller doubts that the effect of the Ebola outbreak is still what weighs most heavily on the minds of anti-immigrant or anti-refugee advocates. More likely, he said, those fears are now motivated by terrorist attacks they see on the news.

“After all, the threat of disease is just one of several distinct kinds of threats that can have important implications for intergroup prejudice and political attitudes,” he said. “There is plenty of research showing that xenophobic responses to ‘outsiders’ are often motivated by fear of physical violence.”

Instead, the behavioral immune system works more subtly. As Murray found, even outbreaks that occurred a century ago can tweak cultures to be slightly less open and a tad more suspicious.

Ironically, although the behavioral immune system is subconsciously aimed at keeping us healthy, it might actually be bad for our health. Fear of foreigners is what prompted villagers in areas stricken by the Ebola outbreak in 2014 to block the paths of health workers, after rumors swirled that outsiders were the ones responsible for the disease. In the words of one young man in Kolo Bengou, Guinea: “We don’t want any contact with anyone.”

People with overactive behavioral immune systems might also socialize less, since they are less likely to be extroverted and open. “There’s a ton of research showing that social isolation has negative health consequences later in life,” Murray said.

Even as they dodge infectious diseases, in other words, these individuals might be struck by ailments associated with loneliness, like heart attacks and strokes. And that’s the cruelty of the behavioral immune system: It quarantines us to keep us healthy, but life without people makes us sick.