IT IS less than four years since the homicidal zealots of Islamic State (IS) stood on the doorstep of Baghdad, their black flag already fluttering over several other Iraqi cities. The jihadists triumphed, albeit temporarily, because disgruntled Sunnis, former Baathists and others who felt alienated by the rule of Nuri al-Maliki, the Shia prime minister, stood aside. The central government lost control over much of the country. The independence-minded Kurds in the north watched while Iraq fell apart—until IS turned on them, too.

Today things look very different. Iraq has defeated IS and avoided the wave of Shia-on-Sunni violence that many predicted would follow. The number of civilians killed each month in fighting is a fraction of what it was in 2014. The government in Baghdad saw off a premature Kurdish push for independence last year. Oil production is up and the state has money. The power of foreigners, including Iran and America, has diminished as Iraqi politicians have learnt how to play one off against the others. In six weeks Iraq will hold an election, affirming its status as the only Arab democracy east of Tunisia.

Iraq, in other words, is doing well (see article). Some will argue that this justifies America’s invasion to overthrow Saddam Hussein (which we supported). It does not. Too much blood was shed along the way in Iraq and elsewhere. America botched the occupation, touching off a brutal Sunni insurgency. Then Iraq’s politicians stoked sectarian divisions, leading to yet more violence. They must learn from these mistakes, or they will waste this hopeful moment.

Iraq now looks much as it did in 2010, another election year, shortly after the defeat of al-Qaeda in Iraq, the precursor to IS. That victory was thanks largely to America’s support for “awakened” Sunni fighters, many of whom were repelled by the jihadists’ brutality. The Kurds, at the time, co-operated with the government in Baghdad. But after Barack Obama pulled most American soldiers out of Iraq in 2011, Mr Maliki locked the Sunnis out of the security services, cut off funds to the Kurds and jailed Iraqis who complained.

Jobs for men with guns

Today’s prime minister, Haider al-Abadi, is better. A Shia who is nonetheless popular with Sunnis, he has a chance to unite his country. Mr Abadi should merge the militias that helped vanquish IS into Iraq’s regular security forces. He should split the militiamen up and pay them directly, not through their leaders, in order to make them loyal to the state. The elderly could be pensioned off, the young dispatched to college and those who had jobs sent back to work.

Sectarianism must be stamped out of politics, too. Since the invasion, Iraq’s leaders have done deals that guarantee most parties a share of power and its spoils. This has led to corruption and stagnation, not unity. Jobs are handed out by sect and ethnicity, not merit, and ministries are plundered. The state is so dirty that many Iraqis have come to doubt the merits of democracy. No opposition exists to hold the executive to account.

Lately, parties have delighted Iraq’s increasingly secular voters by forming broad coalitions that campaign on issues. They briefly did the same in 2010. The test of this will come after the vote. The winners, having no doubt promised to tackle corruption, should do so. Ditto for vows to keep the peace and govern for all. With luck, a more normal Iraqi politics will emerge, based on policies and competence, not sect.