The world is moving to the city. Fifty-four percent of the seven billion people now inhabiting our planet live in an urban area, and by the middle of the century, that number is expected to rise to 66 percent.

Many of these people will be living in so-called megacities—urban areas with populations greater than ten million. Today there are only 28 such cities in the world. By 2030, there are expected to be 41.

Nearly all that growth will happen in Asia and Africa, according to U.N. projections, and 37 percent of the world’s urban expansion is expected to happen in just three countries: Nigeria, India, and China.

Urbanization helped create large, stable middle classes in the U.S. and England after the Industrial Revolution, and the hope is that it will do the same for the developing world of the 21st century. Nigeria already shows positive signs. In 2008, the World Bank changed Nigeria’s income status from low to lower middle, and it now has the biggest economy in Africa. The consumer class is actually rising across the continent. According to a recent report by McKinsey & Co., there are now more middle-class families in Africa than India.

China, on the other hand, presents a case study in the downside risk.

Perhaps the most crucial impediment to China’s urbanization program is its hukou system, an ancient family-registration program that was transformed after the Communist revolution of 1949 into an instrument of the command economy. Its purpose was to control the national workforce, mainly to keep rural workers from flooding into the cities. Rural workers who lacked a government permit to move to the city were denied access to public services there, including education and health care.

Pressure for reforms is growing, but so far rural workers have been allowed to migrate only to China’s towns and smaller cities, many of which suffer from inadequate services and infrastructure. China already has at least five of the world’s megacities and a dozen more within striking distance. In these cities, at least for now, rural workers remain a socially and legally segregated class.

On a panel this spring at the Brookings Institution in Washington, Hu Biliang, dean of the School of Development Studies at Beijing Normal University and a proponent of hukou reform, prescribed two other fixes for China’s urbanization policy: to improve public services in the small to midsize cities and to let the market influence the process and speed of urbanization. Meanwhile, China continues to build out new megacities much faster than they are filling up, resulting in “ghost cities,” where occupancy rates take years to rise.

That said, they are rising. According to a report by Standard Chartered Bank, at least three of China’s ghost cities sprang to life between 2012 and 2014, with one doubling, another quadrupling, and the occupancy rate in a third growing from 20 percent to 50 percent. Half-occupied may still seem semi-ghostly, but this is China, where one of its designated “ghost cities,” Changzhou, is already home to 4.5 million people.

Next: The Immigration Fix