Animals produce plenty of weird compounds. Take, for example, squid ink, skunk spray or even hagfish slime. But one of the strangest animal-produced byproducts is goldfish alcohol. When our finned friends are in low oxygen environments, like at the bottom of a frozen pond, goldfish and related carp species produce alcohol from their gills. Now, as Ryan F. Mandelbaum at Gizmodo reports, researchers have finally figured out how and why the critters produce this fishy moonshine.

For most vertebrate animals, when oxygen is no longer available, the body switches to anaerobic respiration, which it quickly breaks down carbohydrates for energy, reports Rachel Baxter at New Scientist. But similar to how sprinters can only maintain their zip for short distances, fish can only rely on this process for a short time due to the buildup of lactic acid, which is dangerous in high concentrations.

Goldfish and crucian carp, however, metabolize those carbs differently than other animals when oxygen is scarce. The creatures convert these carbs to ethanol, which they expel from their gills. This means the lactic acid does not build up in their bodies, allowing them to survive in the low oxygen environment.

Just how that happens, however, has long been a mystery. But a study published this week in the journal Scientific Reports is helping to explain the fishy puzzle.

As Mandelbaum reports, to study the fish a team of researchers at the Universities of Oslo and Liverpool put crucian carp in a “goldfish hotel,” a set of airless fish tanks, where they studied them for seven days, taking tissue samples from the fish.

The researchers found that the muscle tissue of the fish contains two types of enzymes that funnel carbohydrates to mitochondria, the cellular powerhouses where energy is produced, according to a press release. One set of these proteins follows the normal metabolic pathway. But in a low-oxygen environment the second enzyme called pyruvate decarboxylase turns on, processing the metabolic waste to produce the less-dangerous ethanol, which is then cleared from the fish’s system. It's a bit like how brewer’s yeast makes the good stuff, Baxter notes.

During the extended periods of ice cover in northern Europe, “blood alcohol concentrations in crucian carp can reach more than 50 mg per 100 millilitres, which is above the drink drive limit in these countries,” co-author , evolutionary physiologist at the University of Liverpool, says in the press release. “However, this is still a much better situation than filling up with lactic acid, which is the metabolic end product for other vertebrates, including humans, when devoid of oxygen.”

As Baxter reports, the researchers also sequenced the animal’s DNA, finding that the booze-brewing mutation evolved in the ancestor of carp and goldfish about 8 million years ago. The little trick arose due to a mutation known as whole-genome duplication, in which the species has an entire extra copy of genetic material. A mutation in those duplicate genes gave the fish their special trick.

It’s also a pretty impressive survival adaptation. “The ethanol production allows the crucian carp to be the only fish species surviving and exploiting these harsh environments,” lead author Cathrine Elisabeth Fagernes of the University of Oslo says in the release, “thereby avoiding competition and escaping predation by other fish species with which they normally interact in better oxygenated waters.”

So the big question is, do the fish actually get drunk? Berenbrink tells Mandelbaum that it’s kind of hard to tell. “Under the ice they try to minimize the energy expenditure,” he says. “In a way the behavior changes because they’re just sitting there. We can’t really distinguish if that’s from the alcohol or the survival strategy.”

The next step is comparing the various alcohol-producing-species to find any differences in the process and to figure out just when and how the ethanol production clicks on and off.