FOR a wonderful few days, it seemed that Libya’s new Government of National Accord (GNA), headed by Fayez al-Serraj, the prime minister, was on the verge of a momentous victory last week. Forces aligned with Mr Serraj had driven the jihadists of Islamic State (IS) back over 100 miles. They then captured the airport and seaport of Sirte, the group’s stronghold and the hometown of Muammar Qaddafi, the Libyan dictator overthrown (and killed) in 2011. The jihadists were pinned down in the city centre. “The operation will not last much longer,” said Muhammad Ghassri, the GNA’s spokesman, on June 9th.

But up to that point, IS had not put up much resistance. Now the jihadists are hitting back in an attempt to retake the port and other areas. Hundreds of its fighters, many from abroad, remain holed up in Sirte. The GNA’s offensive has stalled. Its forces, made up mostly of militias from Misrata, in the west, have thus far shown a willingness to take casualties. More than 100 of their men have died and some 500 have been injured. But in order to clear Sirte of jihadists, more sacrifice will be needed.

The fighting has certainly hurt IS, which had used its control of roughly 180 miles (290km) of coastline around Sirte to bring in supplies and new recruits. It has now lost almost all of that territory—the latest in a string of setbacks for the jihadists. In February militias backed by American air strikes destroyed its base in Sabratha, in the west. Last year local forces kicked it out of Derna, in north-east Libya. The group’s collapse has led some to doubt previous estimates that it had some 6,000 fighters in Libya, which was seen as a growing jihadist hub.

Many of the group’s leaders are thought to have slipped out of Sirte and gone south. The GNA had threatened its offensive for weeks, so the jihadists knew it was coming. Though aided by American and British soldiers, who are helping with logistics and intelligence, the GNA’s forces failed to secure all of the routes out of Sirte. That would have needed better co-ordination with local militias. “Even if Sirte is liberated, that does not mean that IS is gone from Libya,” says Jason Pack of Eye On ISIS in Libya, a monitoring service.

The offensive may also be intensifying rivalries in Libya, which has been mired in a hot and cold civil war for over two years. The two-month-old GNA, which is backed by the UN and based in Tripoli, the capital, has largely displaced the former government in the west. But it still has not won the support of a rival government in the east. The Libyan National Army, which is led by Khalifa Haftar and aligned to the eastern government, has not participated in the battle for Sirte. The parliament in the east, which must approve the UN-sponsored agreement creating the GNA, says it will vote on the deal—a promise it has made before.

The GNA’s quick advance has, for now, weakened Mr Haftar’s claim to be the West’s best hope of defeating Libya’s jihadists. He looms over the eastern government and is often considered a spoiler of efforts to unify the country. Some say he is losing recruits to the GNA. But it too has been exposed. Its fighting force, drawn from the western town of Misrata, is much the same as the one that backed the old western government and battled Mr Haftar. “It is not a unity government, just a rebranding of Misratan militias,” says Mr Pack. Their loyalty can be fickle. Last year they fought another militia that is now attacking IS from the east.

Despite controlling state institutions, such as the central bank and the national oil firm, the GNA has struggled to establish its authority. It has also been reluctant to assert it in some areas, so as to avoid fuelling separatist sentiment in the east. The offensive against IS, if successful, may rally more Libyans to its side. The UN also hopes to give it a boost: on June 14th the UN Security Council authorised a European naval mission to enforce an arms embargo on Libya.

But there is also now the risk of the jihadists resorting more to terrorism, as they have done elsewhere in the region. An attack in Tripoli or on state infrastructure could quickly undermine the gains the GNA has made. IS was able to gain ground in Libya because a lack of unity led to chaos. That underlying problem still has not been solved.