Cicadas can remain underground for as long as 17 years before emerging into the sunlight, where they survive for just a few weeks, enough time for the insects to mate and lay eggs. But right before they crawl out of the earth, some cicadas get infected with various species of the fungus Massospora—and things start to get freaky. The fungus takes over the cicadas’ bodies, eating through their limbs. It also makes the males sex-crazed. They frenetically try to mate with anything they can find, even after their genitals and butts have literally fallen off.

Now, scientists think they have uncovered chemical mechanisms that drive this buggy horror story. According to a new study in the journal Fungal Ecology, a team of researchers reveal that certain species of Massospora produce psychoactive compounds as they infect their unfortunate victims.

Fungi and other animal parasites often take control of their hosts’ behavior; it’s a good way to increase transmission of the disease. Sometimes, insect-infecting fungi cause critters to ascend to the top of plants before they die, “which facilitates post-mortem dissemination of spores later emitted from their mummified carcasses,” the study authors write. What happens with the cicadas is less frequently seen: They spread Massospora fungi while they are still alive.

After they get infected, the cicadas’ abdomens may eventually slough off, revealing a white fungal “plug” that sprinkles its spores when the host flies around or mates. And boy, do infected cicadas mate a lot. Males will try to copulate not only with females, but also other males. Not even losing parts of their bodies, including their genitals, slows the lusty cicadas down.

“[T]wo-thirds of their body might be missing, and they would be whistling as they walk down the street,” Matthew Kasson, study co-author and plant pathologist at West Virginia University in Morgantown, said during a meeting of the American Society for Microbiology, according to Tina Hesman Saey of Science News.

To find out what might be driving this bizarre behavior, the research team analyzed Massospora-infected cicadas collected from the wild. They discovered that one Massospora species, Massospora cicadina, produces cathinone, an amphetamine that previously had only been detected in plants. Cathinone is potent stuff; as Sarah Sloat of Inverse points out, synthetic cathinones are the stimulants more commonly known as “bath salts.”

What’s more, the team’s analysis of cicadas infected with Massospora platypediae and Massospora levispora revealed that the fungi were producing psilocybin, the hallucinogenic chemical found in certain types of mushrooms. In fact, this study marks the first time that psilocybin has been found in any fungi except mushrooms.

The study not only offers an explanation for infected cicadas’ wacky behavior, but may also open up new avenues of pharmacological research. “These psychoactive compounds are medicinally important,” Kasson tells Vice’s Madeleine Gregory. A growing body of research suggests that controlled doses of psilocybin, for instance, could help treat mental health conditions like depression and PTSD.

And in case you were wondering: Yes, it might be possible to feel the psychoactive effects of Massospora-infected cicada “if you’re motivated enough,” Kasson says. But he also notes that his study found hundreds of other compounds in the cicadas, some of which might be harmful to humans. So, Kasson concludes, “I wouldn’t take that risk.”