If you’ve ever wondered why some mushrooms are considered to be “magic” while others are somewhat boring in comparison, you may have come to the conclusion that the magic mushrooms might just be smarter than the others. As a recent study has demonstrated, it turns out you might be right!

The study found that psilocybin, the chemical responsible for the psychedelic effects of more than 100 strains of fungi, may have originally been used by the mushrooms to intentionally deter pests from eating them.

A research team at Ohio State University compared several strains of mushrooms containing psilocybin with those that did not and found that the psilocybin mushrooms shared a cluster of five genes that the non-psychoactive fungi did not possess.

Although they are not closely related, all psilocybin mushroom species share a loving affection for animal feces and rotting wood. It turns out that these particular habitats play host to many species of insects and small creatures that are eager to feed on mushrooms. When humans consume psilocybin, they can experience psychedelic effects that change the way the world is perceived. However, it turns out that when insects consume psilocybin they may not start tripping after all—instead, the study’s authors posit that the chemical may be tricking insects into losing their appetite.

The mushrooms developed the ability to share this pest control technique through an evolutionary process called “horizontal gene transfer.”

Horizontal gene transfer is simply the movement of genetic material between organisms other than solely from parent to child. This is how bacteria share genes to resist antibiotics, for example. While this tactic is quite common in single cell organisms, it’s incredibly rare for more complex species like mushrooms to employ it.

The process is often prompted by a stressor, such as an attack by a predator or in response to a life-threatening virus. It seems that attempts by insects to try and consume mushrooms may have been the catalyst for the mushrooms to create psilocybin.

Plants are well known for their ability to develop chemical compounds that act as defense mechanisms. For example, the geranium produces a chemical called quisqualic acid in its flowers that can paralyze a Japanese Beetle within 30 minutes after ingestion. The chemical wears off after a few hours, but during that time the beetle will typically be consumed by its own predators.

It’s intriguing to think that psilocybin may have been developed to deter predators from attacking mushrooms rather than to enable human beings to go on psychedelic voyages. However, we don’t yet know for sure—it’s even possible that the mushrooms had both results in mind when they started creating the psychoactive chemical.Understanding magic mushroom genes could lead to new uses for psilocybin. There has already been a lot of promising research involving this chemical, including potential treatments for depression, smoking cessation, and treating end-of life anxiety among patients with terminal cancer. For now, this study suggests an alternative genetic motive for the birth of psilocybin, one that is certainly worth considering.