After a flood, thousands of homeless fire ants pile on top of one another to build bizarro towers with their own bodies — probably as temporary shelters. All the complex construction project needs is for each individual ant to follow a few simple rules, new research says. And, if wriggling ant towers weren’t nightmarish enough, this discovery could help engineer robot swarms that are even better at cooperating.

Fire ants are native to the wetlands of Brazil, so they’re accustomed to torrential rainy seasons. When their nests inevitably flood, thousands of ants rapidly link up with one another to form floating rafts. And when the raft drifts to solid land, the ants clamber up around a nearby blade of grass or stick to build a bell-shaped structure with their own little bodies. So it’s kind of like a little Eiffel tower, but for ants.

Scientists think that these towers act like makeshift shelters until the ants can build more comfortable accommodations. Craig Tovey, an engineering professor at Georgia Tech, wanted to know how they do it. “Ants are not all that smart,” he says. They can’t see that well. They can’t know how many other ants need to fit into this structure. And they have no master blueprint guiding them. “You don’t expect them to be capable of a huge variety of tasks — so it was weird to see these towers.”

So, Tovey and his team slipped the ants radioactive tracers and used X-ray video to peek inside the ant towers as they formed. Fortunately (for the researchers, and absolutely no one else) fire ants have invaded Georgia. So, whenever Tovey’s team needed a fresh batch of fire ants, they headed outside with water to flood a nest, and a bucket to collect its fleeing denizens. The trick is to use a bucket with a lid, Tovey says. Otherwise, “they will climb up your shoes and bite your legs.”

The researchers discovered that these towers are constantly sinking and being rebuilt as ants crawl up the sides and descend down the middle, according to the study published Tuesday in the journal Royal Society Open Science. It’s kind of like a disgusting ant-flavored chocolate fountain flowing in reverse, Tovey says. By pairing the X-ray videos with mathematical models, the scientists discovered that each individual ant shuffles around until it’s being squished by no more than three other ants. And the ants do this by just following three simple rules:

Don’t move if there are other ants on top of you

If you are on top of other ants, keep moving in random directions

If you find an open parking spot next to other immobile ants, pull in and link up with your neighbors

This is the first time ants have been filmed building their towers, Guy Theraulaz, an animal behavior researcher who was not involved in the study, told Nature. And Tovey hopes that his team’s findings could help program swarms of robots to one day work together in rescue operations, or to cross chasms and build structures on other planets. Of course, when these robot swarms inevitably turn on us and return to ravenously devour the Earth, we’ll know who to blame.

Update July 19th 7:50AM ET: This article was originally published on July 12, 2017 and has been updated to include video.