NEXT month around two dozen Formula One racecars will speed through the gleaming centre of Baku, the capital of Azerbaijan. They will zoom along the shore of the oil-rich Caspian Sea, past five-star hotels and shops displaying Brioni suits and Chopard watches. They will also race past the less attractive side of Azerbaijan: its sputtering economy, oppressive political system and simmering conflict with Armenia. At such speed, it will be hard to spot the signs of insecurity in this former Soviet republic of 10m people squeezed between Iran, Turkey and Russia.

Formula One’s European Grand Prix is a fitting vanity project for Azerbaijan. The country has transformed itself from a failing state in the early 1990s to a rich and corrupt oil economy. Between 2003 and 2015 oil and gas revenues were $119 billion; the cash was spent on infrastructure, weapons and ostentatious follies. Some of the money the country earned from oil and gold mines has been funnelled into powerful people’s offshore accounts. But enough of it trickled down to fuel strong domestic demand, largely satisfied by imports.

When the oil price crashed, the music stopped. Baku’s taxi-drivers now grumble about rising prices and the money being wasted on the Grand Prix. Azerbaijan had to devalue its currency twice last year, after its central bank burned through some $10 billion of foreign-currency reserves trying to defend the manat. Banks are weighed down by some $2 billion of unpaid loans, say some analysts. The State Oil Fund of Azerbaijan (SOFAZ), which takes in all of the country’s oil and gas revenues, is still sitting on $35 billion—roughly equal to a year’s GDP—but spending has been cut back and the budget now conservatively assumes a price of $25 a barrel. Azerbaijan has also been hit by the recession in neighbouring Russia, where hundreds of Azerbaijanis once worked. The economy was 3.5% smaller in the first quarter than a year earlier. Inflation is in double digits. Rising bread prices caused riots in several regions, quickly pacified with cash and police truncheons. Azerbaijan’s political clans, who control much of its economy, have been jolted out of their comfort zone. For much of his rule the legitimacy of Ilham Aliyev, the autocratic president who inherited his post from his father, rested on Western-operated oil and gas projects. Now he must choose between modernising the country or becoming more dictatorial. Spooked by the Maidan revolution in Kiev in 2014, Azerbaijan copied some of Russia’s repressive practices. Non-governmental organisations were deemed agents of foreign influence; dissidents were jailed. But Russia’s annexation of Crimea and the war in eastern Ukraine worry Mr Aliyev, too. Azerbaijan’s Nagorno-Karabakh region is occupied by Russian-backed Armenia (see article), and has long resisted the presence of Russian troops in the region. Struggling to cope with cheap oil and the need to finance energy projects, Mr Aliyev is now seeking to repair relations with the West.

Shortly before his trip to Washington, DC, for a nuclear-security summit in March, Mr Aliyev released a number of political prisoners. But he kept two prominent ones in jail: Ilgar Mammadov, leader of REAL (“republican alternative”), a pro-Western opposition movement; and Khadija Ismayilova, an investigative journalist and anti-corruption activist. To dodge the security services, REAL cells now masquerade as cyclists’ associations or book clubs. Natiq Jafarly, the group’s secretary, says there has been an inflow of new members from small businesses and even from the state oil firm, Socar.

To appeal to the middle class, Mr Aliyev is trying to modernise the country while maintaining a tight grip on politics. He has launched a “one-stop shop” public-services bureau that largely eliminates the need to pay bribes for official documents. Mr Aliyev has sacked the chief of Azerbaijan’s security service, the organisation which succeeded the local KGB (once headed by Mr Aliyev’s father). He has also promised to reform the economy. “We were planning to do reforms in about ten years’ time. Now we have to do them a lot sooner,” says Natiq Amirov, Mr Aliyev’s assistant for economic reforms. The country has built plenty of infrastructure, he says; what it lacks is human capital. Azerbaijan has been talking about diversifying its economy for years. Now that “empty rhetoric” is starting to inch towards reality, says a Western diplomat. The model for the reforms is Malaysia. Much of the elite believes it can import economic reforms without touching the political system—just as London-style taxis have been brought over to cruise Baku’s streets, and the first lady’s favourite restaurant has been imported from Marbella, chef, cutlery and all. Azerbaijan lacks a bureaucracy capable of reform. (The very word scares public-sector workers, says Mr Amirov.) But it does have bright young Western-educated talent. Taleh Ziyadov, who holds a doctorate from Cambridge University, heads the free-trade zone at Baku’s new port, a point on China’s “One Belt, One Road” infrastructure link to Europe. “We don’t have time to wait for the whole country to reform,” says Mr Ziyadov. The training ground for this new elite is the Azerbaijan Diplomatic Academy (ADA), a leafy, modern campus next to a zoo. The curriculum is entirely in English; many of the staff are international. Its director, Hafiz Pashayev, is the 75-year-olduncle of Azerbaijan’s first lady and a former ambassador to Washington. Its mission, he says, is “to create a special milieu where students are free to think”.

Mr Pashayev, a former Soviet physicist, is well aware that ADA’s graduates will eventually press the existing elite for more power, just as the Soviet intelligentsia did in the 1980s. But as a member of one of the country’s powerful families, he hopes to foster a managed succession of the elite, rather than a radical break of the kind that happened in Georgia or Ukraine.

That a clan-based autocracy can pull off such a modernisation programme seems doubtful, but nationalism is a strong motivator. “Our objective is to propel the country to the top position in the world,” says Mr Pashayev grandiosely. ADA might succeed. But in a country of oil wells and fast cars, the lure of rent-seeking and corruption is strong.