IT IS less a chaotic rout than a tactical retreat, but suddenly Islamic State (IS) is losing ground quite fast. At both ends of its self-proclaimed caliphate, the jihadist group is ceding territory. It quietly withdrew from Tal Afar, the largest Iraqi city still under its control, on August 27th.

Simultaneously, hundreds of IS fighters and their families emerged from their caves in the Qalamoun mountains, astride Lebanon’s border with Syria, and boarded buses heading east. Trapped between Syrian and Lebanese forces on the ground, and Russian bombardment from the air, they gave up after a weeklong battle.

Increasingly, the jihadists are being squeezed into a ribbon along the Euphrates valley. Having lost the Iraqi city of Mosul, and on the retreat in the Syrian city of Raqqa, their last redoubt is likely to be the city of Deir ez-Zor, in eastern Syria. But here, too, they are vulnerable: the Syrian army retains control of two enclaves on the western edge of the city.

The acceleration of the jihadists’ retreat has taken everyone by surprise. Tal Afar had served as the garrison linking Raqqa, the caliphate’s putative capital, with Iraq. Several of its leaders were Sunni Turkmen from the city—two were deputies of Abu Bakr al-Baghdadi, the self-declared caliph. Many of its feared religious police, the hisbah, were also from Tal Afar. They led a massacre of the Yazidi minority below the nearby mountain of Sinjar and helped run IS’s ghastly trade in sex slaves.

Iraq’s army, therefore, expected to face fierce resistance from 2,000 or so jihadists in Tal Afar. Instead it met far fewer. The contrast with Mosul, where nine months of street-by-street combat left the city in ruins, is stark. Observers who entered Tal Afar after the battle found few dead bodies. The city centre was largely unscathed. Fewer than 100 Iraqi soldiers died in the fighting, says an official, compared with many thousands in Mosul.

This is partly because of topography. In Tal Afar, Iraq’s soldiers did not have to squeeze through tight alleyways, as they did in the old city of Mosul. They also offered the jihadists escape routes. Hundreds of fighters are said to have passed through the lines of the Iraqi army and Kurdish forces, known as the Peshmerga. “The borders are simply too porous,” said an Iraqi official. In Lebanon, IS fighters negotiated with Hizbullah, a Shia militia, to arrange a safe passage eastward out of their mountain redoubt, in exchange for returning the corpses of Lebanese soldiers. American forces have blocked them from joining up with their comrades in Deir ez-Zor.

But even where there are no such exit routes, IS is giving up ground. The Syrian Democratic Forces, a joint Kurdish and Arab force trained and armed by America, have taken two-thirds of Raqqa since launching an assault on the city in June. IS affiliates are in retreat in southern Syria, around Deraa, too.

The jihadists can still tap into seething sectarian resentment. Displaced Sunnis lament the levelling of the old cities of Mosul and Raqqa, and say Tal Afar was spared only because it has many Shias. Over 700 civilians have been killed by American air strikes and artillery shells in Raqqa, according to Airwars, a monitoring group based in London. Some 20,000 civilians are said to be holed up with as many as 2,500 jihadists in the old city. IS still has some strength to counter-attack. On August 29th it captured a tank and heavy artillery in Raqqa. It appears to be making a stand in the town of al-Ayadiya, outside Tal Afar. And it can always resort to terrorism: Karrada Dakhil, a favourite shopping street in Baghdad, has again been closed to cars for fear of bombs. But as a territorial state IS appears spent. The administration and provision of services, of which it once boasted, have all but collapsed in much of the territory still under its control. Electricity and running water have vanished. Food stocks are thin. Medical care in Raqqa is reserved for fighters. “IS does not treat civilians,” says an anti-IS activist in the city. “They die of their injuries.” Families flee, defying the group’s orders. They must dodge jihadist snipers and avoid landmines laid to keep them hostage. Still, Iraqi soldiers pushing into Tal Afar found most homes empty. Fighting on many fronts, IS finds itself stretched. In one district of Deir ez-Zor the group has cut the number of religious policemen as it diverts men to the battlefront. It has also sharply reduced its suicide-bombings in and around Raqqa. For the first time it is enforcing military conscription on men between 20 and 30 in Deir ez-Zor. The pullout from Lebanon and Tal Afar should bring reinforcements to IS in Syria, but internal discipline seems to be ebbing, too. Local IS fighters are complaining more loudly that foreigners still enjoy free food and health care, whereas they face wage cuts. “On the front line fighters don’t obey the rules any more,” says a former militant, who notes that many smoke. Religious zeal is no longer enough to spur the jihadists into action. In the caves IS abandoned in Qalamoun, Lebanese soldiers found Captagon pills, a stimulant often taken before battle.

Months of fighting may yet lie ahead, but the trajectory is clear. As its forces advance, Iraq recently reopened its border with Jordan at Trebil. Syrian government forces are pushing east from Palmyra towards Deir ez-Zor. IS’s leaders are probably hiding in riverine towns such as Abu Kamal and al-Mayadin, as they prepare for their last stand.