A WEEK after a general election rocked by suspicions of fraud, the dust is beginning to settle. It looks all but certain that Imran Khan, a former captain of Pakistan’s cricket team, will be sworn in as the country’s next prime minister. His party, Pakistan Tehreek-e-Insaf (PTI), will dominate the legislature. The outgoing Pakistan Muslim League-Nawaz (PML-N) and the Pakistan Peoples Party cried foul, noting that the army had come out strongly in Mr Khan’s favour, muzzling the press and sending security agents to meddle in polling stations. But the fact that these two ancient rivals are now making common cause as the loyal opposition suggests that they accept the result. Few Pakistanis want endless street protests and political turmoil. So the democratic show rolls on. For the second time in Pakistani history, power has been democratically transferred.

For over two decades Mr Khan has railed against a sleazy system of hereditary politicians and patronage networks. Yet this is the first time the PTI has shown a broadly national appeal in a country of 207m. Its 4m more votes than the PML-N represent a notable popular victory, one only partly undermined by vote-rigging allegations. Most remarkable is the PTI’s win in Karachi, a city of powerful local machines and thuggish street politics. The PTI may yet wrest Punjab, the country’s bread basket and most populous province, from the PML-N. That would cap a remarkable fall for the Sharif brothers: Nawaz, the “Lion of Punjab”, who was prime minister until last year and is now in jail facing corruption charges, and Shahbaz, Punjab’s former chief minister.

Yes, he Khan

Mr Khan, who now commands about 115 seats in the National Assembly, still needs a handful of allies—independents and smaller parties—in order to govern. The PTI’s chief bankroller, Jahangir Tareen, has been flattering independents by flying them in to Islamabad, the capital, on his private jet. The grubby promises to them are the kind of thing Mr Khan used to decry. His wooing of the Muttahida Qaumi Movement, or MQM, the most unsavoury of Karachi’s parties, is making some PTI leaders gag. But at least it means that Mr Khan does not need radical Islamist parties to form a governing coalition. Before the election, he pandered to zealots.

Meanwhile, over the economy there is no time to lose. Not for the first time, an incoming government faces a balance-of-payments crisis. The current-account deficit has widened and the currency is sliding. Pakistan imports three-quarters of all its energy needs. Yet foreign-exchange reserves are down to just $9bn—barely two months’ import cover. An IMF bail-out, of perhaps $12bn, looks all but inevitable. Negotiating one will require finesse. Pakistan is in hock to China which, through a ballyhooed China-Pakistan Economic Corridor, has promised $62bn of infrastructure spending. This week America’s secretary of state, Mike Pompeo, insisted that his country would block any IMF bail-out that profited China. The task of stitching a deal together will fall to Asad Umar, Mr Khan’s probable finance minister. He is a good choice: the former head of Engro, the country’s biggest private conglomerate, Mr Umar is reform-minded and admired.

The terms of an IMF deal will bring a populist party down to earth—so much for Mr Khan’s wild promises of an “Islamic welfare state”. The next challenge is the electricity sector. In office, the PML-N did much to fix Pakistan’s notorious blackouts, helped by China building new capacity. But a tangle of debts among state generators, energy suppliers and banks has been exacerbated by theft from the grid. This can be resolved by reducing subsidies, raising energy taxes and recapitalising state entities. Mr Khan has long bemoaned Pakistan’s institutional decay. Renewal starts with fixing the electricity mess.

Then come security and foreign policy. Islamist violence marred the election and is a constant threat to Pakistan. Meanwhile, the regional situation grows trickier, with rivalry between America and China, and China and India. That comes on top of rocky relations with America itself, the festering sore of war-torn Afghanistan to the north-west, and Pakistan’s age-old and bitter animosity towards India.

Mr Khan would seem ill-suited for these challenges. He has been more critical of America, especially over its use of drones to kill jihadists, than of the extremists themselves. And he is pally with an army that is the chief obstacle to better relations with India, the sworn enemy.

There is room for surprises, though. Under Nawaz Sharif, the civilian government and the army clashed. The generals distrusted, and then thwarted, Mr Sharif’s overtures to his Indian counterpart, Narendra Modi. Relations with India, they make it clear, are their remit. But perhaps, says Sehar Tariq of the United States Institute of Peace, “harmony” between civilian rulers and the army (ie, civilian subservience) could “reap dividends” over India. High-level exchanges have recently taken place between the two countries’ armed forces. Pakistan’s army chief, General Qamar Bajwa, is relatively doveish towards India, acknowledging that home-grown jihadism is a far greater threat to Pakistan. Pakistan, via the generals, may yet find the will to seek better Indian ties.

One day, though, Mr Khan will surely clash with the generals. He speaks of opening the border with Afghanistan, an idea at odds with the 2,300km-long fence the army wants to build. And he wants to spend heavily on health and education, money which can only be found by crimping the armed forces’ budget. Farooq Tirmizi, an analyst, predicts a fight that will come down to “guns versus textbooks”.

But that is for the future. For now, Mr Khan, who has seldom attended parliamentary sessions and who has described the assembly as “the most boring place on earth”, must find a sense of dedication, detail and compromise that has evaded him till now. He must learn to work with a political class he has only slammed. And he must gently let down his most enthusiastic supporters from the irresponsible highs he generated for them—for instance, by promising to end corruption within 90 days. It will require dogged strength, which he has in abundance, and humility—which, equally, he lacks. Over to the captain.