By all rights, the urban experiment that began in the 19th century should have failed. By the middle of the century, writes the historian Michael Haines, big American cities had become “virtual charnel houses,” their primary demographic characteristic being high mortality. Deaths outnumbered births. Despite the greater availability of food and paid work, children under the age of 5 who lived in cities died at nearly twice the rate as those living in the countryside. In 1830, a 10-year-old living in a small New England town could expect to see his or her 50th birthday—but that same child, living in New York, would be dead before the age of 36.

Even those who survived suffered the price of urban living. Their poor health stunted their growth: The average height of West Point cadets declined by a half inch between 1820 and 1860, as the nation become more urbanized. Reliably, the shortest recruits came from the most densely crowded cities.

Industrial cities survived because new blood in the form of immigrants kept pouring in to replenish their diminished, dying masses. In the years after the 1849 cholera epidemic in New York, immigrants continued to stream into the city, at the rate of nearly 23,000 every month—more than enough to replace the parade of corpses flowing out of the city.

In the later part of the 19th century, new regulations on housing began to slowly ease the deadliness of the city. The crusading journalist and photographer Jacob Riis used the new technology of flash photography to capture images of the dark corners of the tenement world for an aghast public. His 1889 book How the Other Half Lives helped ignite a movement for tenement reform in New York. One of the first reforms, the Tenement House Act of 1901, required that city buildings provide exterior windows, ventilation, indoor toilets, and fire protections.

Some of the city’s neighborhoods didn’t survive this era of housing reform. For example, much of Five Points—an overcrowded Manhattan neighborhood that resembled others the anthropologist Wendy Orent classified as “disease factories”—was simply demolished. A sliver of the old neighborhood became what is now Chinatown. A similar area, the site of the old Collect Pond, became a small paved park fenced with chain link, surrounded by imposing government buildings: Superior Court, City Hall, and the clinics of the Department of Health of the City of New York, among others. Passersby would never suspect that a riotous neighborhood of any kind once existed there.

* * *

Thanks to the housing revolution, even the most crowded cities can now be healthful places to live. In general, people who live in cities today live longer than those who live in rural areas. Only a few health burdens remain, like higher rates of obesity and more exposure to pollution.

And yet, though cities like New York may now appear to be largely washed clean of their past, the change they enjoyed has been partial and selective, passing over many of the poorer countries of the world. In India, due in part to poverty and in part to a lack of governance, housing regulations are as sparse and poorly enforced as in 19th-century New York. The densest streets of Dharavi, a slum of Mumbai, hold 1.4 million people in each square mile, more than seven times the concentration of humans packed into 19th-century Five Points.

And the process of urbanization that began in the industrial era is accelerating. Back then, urbanization was rapid, but it was still limited: Globally, more people lived outside cities than inside them. By 2030, experts estimate, that will change. The majority of humankind will live in large cities. Only a handful of these large metropolises will be healthful and well regulated; many will be more like Mumbai, and two billion of us will live in slums like Dharavi.

The growth of slums is one reason why the 2014 Ebola epidemic was so deadly and long-lasting. Before 2014, Ebola outbreaks had never occurred in towns with populations larger than a few hundred thousand. Just over a hundred thousand lived in Gulu, Uganda, in 2000, when Ebola emerged there; 400,000 people lived in Kikwit, the Democratic Republic of Congo town that experienced an Ebola outbreak in 1995. Since these locales were relatively small and remote, experts widely considered the virus, as the title of a 2011 scientific paper put it, a “minor public-health threat” in Africa.

But then the virus spread into West Africa, where it affected a markedly different demographic landscape. Ebola struck three capital cities, with a combined population of nearly 3 million: Conakry, Guinea; Freetown, Sierra Leone, 165 miles south of Conakry; and Monrovia, Liberia, 225 miles south of Freetown. All three of these cities are overcrowded, haphazardly developed, and chaotic, as scores of news consumers finally learned when lurid photos of West African slums splashed across websites and newspapers during the outbreak.

Crowds provide pathogens like Ebola with at least three advantages. For one, they achieve a sharp uptick in their rates of transmission. When Ebola lurched out of Guéckédou (the Guinean city where the most recent epidemic began) and into the crowded capitals of Guinea and Liberia, its transmission rate spiked. (The same thing had happened to variola, the virus that causes smallpox, when it emerged in urban centers.)

Pathogens can also burn through these larger populations for a much longer period of time. Each of the 21 Ebola outbreaks that preceded the most recent epidemic had been contained within a few months. But 10 months after Ebola struck West Africa and its cities in 2014, the epidemic was still growing exponentially. “We have never had this kind of experience with Ebola before,” said David Nabarro, who was coordinating the United Nations’ response to the epidemic. The nature of the urban landscape spelled the difference. “When it gets into the cities,” he said, “then it takes on another dimension.”

But the most transformative effect of crowds lies in the way they allow pathogens to become more deadly. Under most circumstances, virulence is detrimental to a pathogen’s ability to spread. Consider pathogens that spread when people breathe on each other, like influenza, or when they touch each other, like cholera or Ebola. Successful transmission depends on social contact between infected and non-infected people. If there are no uninfected people around to inhale the breath of the infected or touch their bodily fluids, the pathogen is stuck. It can’t spread.

This reliance on social contact makes virulence problematic for such pathogens. If they are highly virulent, their victims will sicken and perhaps even die. Infected people won’t be shaking hands at work or breathing on other passengers on the train; they’ll end up alone in bed or isolated in hospital wards. And when infected victims die, their bodies may be abandoned, burned, or buried before the pathogens lurking inside can spread to anyone else. This is a serious disadvantage. And it’s why highly virulent strains are more likely than less virulent ones to die out. Virulence is evolutionarily constrained.

But certain human behaviors lift these brakes on virulence, allowing even the most deadly strains to spread and flourish. One example is burial rituals that require the bereaved relatives to handle the corpses of their loved ones. A funeral tradition among the Acholi people of Uganda, for example, calls for corpses to be bathed by relatives and for their faces to be ritually touched by mourners. Similar rituals, which likely played an important role in the 2014 Ebola epidemic in West Africa, free pathogens from the disadvantages of virulence. Even pathogens that promptly kill their victims, like Ebola does, can spread into new victims, because social contact continues even when infected people are dead.

Crowds of people in slums do the same thing. In crowds, social contact continues even when victims are sick and dying. The sickbed is in the living room or the kitchen where friends and relatives have easy access to the ill. The hospital wards are full and beds are crowded with patients, worried relatives hovering at their sides. Under such conditions, pathogens that evolve to become more virulent suffer none of the debilities that virulence would normally exact. They can spread regardless of how ill they make their victims.

They can be as virulent, in other words, as the most dangerous pathogens in the world: those that don’t rely on social contact to spread. These pathogens are either stable in the environment (like cholera, which spreads through contaminated water) or are carried by vectors (like Plasmodium falciparum, which causes malaria and is spread by infected mosquitoes). Virulence doesn’t handicap their ability to spread, because they can spread from their dead victims by persisting in the environment until another live victim picks them up.

Pathogens that spread through social contact, by comparison, are usually destined to be relatively mild. But crowds allow even these pathogens to become killers.

Urban expansion has similarly accelerated the spread of Zika virus now washing over the Americas. For decades, Zika virus lurked in the equatorial forests of Africa and Asia and rarely infected humans, in part because it was carried by a forest mosquito that lived in the jungle and mostly bit animals, not humans. That changed when the virus arrived in the Americas. Here, it's being spread by the urban-adapted mosquito Aedes aegypti,which thrives in cities and only bites humans. As the American tropics have urbanized, Aedes aegypti has dramatically expanded its range—and with it the number of people vulnerable to the plethora of pathogens it carries, from dengue and Chikungunya to Zika.

This article has been adapted from Sonia Shah’s book, Pandemic: Tracking Contagions, From Cholera to Ebola and Beyond.