The crux of the problem is three ancient pillars in the Mina Valley, which pilgrims are required to stone as a symbol of the Devil. Serwill watches on his video monitor as thousands of pilgrims surround the site, a scene he compares to the spin cycle of a washing machine, “pushing and fighting their way to the pillars,” he says. Generally, three to four people can fit into one square meter; in Mecca, 10 fit in that space. “You don’t find these levels of density among humans anywhere else—only among rats,” says Habib Zein Al-Abideen, the Saudi deputy minister of Municipal and Rural Affairs and head of the kingdom’s hajj-related construction. Under such conditions, pilgrims are exposed to pressure equivalent to more than a ton—similar to the weight of a small car.

Working mainly off videos and aerial photographs, the engineers employed cutting-edge computer software to digitally map pilgrim flows so that they could pinpoint the exact moments when disasters have broken out in the past. They used the latest theories in “panic studies” to understand how people react when forced into “escape mode”—much of it applied from rock concerts and football games. Based on these studies, the engineers redesigned the entire hajj process by creating a network of one-way streets, circumscribed plazas, overflow areas and emergency escape routes—applying a strict structure to what has historically been haphazard and chaotic. They also organized what might be the most complex schedule ever attempted, coordinating time slots for 30,000 different groups of 100 pilgrims each.

To further loosen the crush, Serwill and his team helped design the $1 billion, four-tiered Jamarat Bridge. Used for the first time this year, the bridge carries 360,000 pilgrims every hour through the valley; when complete, it will handle 650,000 an hour, accommodating the more than 20 million pilgrims the hajj is expected to attract in the coming years.

The Saudis were skeptical at first. “We heard a lot of ‘We’ve been doing it like that for the last 1,000 years, in that chaotic way, and that worked’,” Serwill says. “They had their doubts about our systems, and at the beginning we really had to convince them it was necessary, that it would work.” The Saudis knew, Serwill says, that if the system failed, “the blame would come to them, not the European consultants.” Ultimately they came around.

The project is part of a wider push by the Saudi ministry to modernize the hajj. Thousands of Saudi Boy Scouts armed with GPS devices are being dispatched to help guide pilgrims between far-flung pilgrimage and camping sites. This year, the government is testing radio-frequency-identification technology that can track pilgrims as a way to improve security and monitor crowds. An intricate network of cameras will allow authorities to watch all the action from a control center, and respond quickly if trouble is brewing. Muhammad may not have needed a GPS device to find his way from Mecca to Medina 1,400 years ago, but he also didn’t have millions of people following him in his footsteps.