Islamic State militants began reinventing themselves months before U.S.-backed Iraqi forces ended their three-year reign of terror in Mosul, putting aside the dream of a modern-day caliphate and preparing the ground for a different fight.
Intelligence and local officials said that, a few months ago, they noticed a growing stream of commanders and fighters flowing out of the city to the Hamrin mountains in northeast Iraq which offer hideouts and access to four Iraqi provinces.
Some were intercepted but many evaded security forces and began setting up bases for their new operations.
What comes next may be a more complex and daunting challenge for Iraqi security forces once they finish celebrating a hard-won victory in Mosul, the militants' biggest stronghold.
Intelligence and security officials are bracing for the kind of devastating insurgency al Qaeda waged following the 2003 U.S.-led invasion, pushing Iraq into a sectarian civil war which peaked in 2006-2007.
"They are digging in. They have easy access to the capital," Lahur Talabany, a top Kurdish counter-terrorism official, told Reuters. As part of the U.S.-led coalition, he is at the forefront of efforts to eliminate Islamic State.
"I believe we have tougher days coming.”
Some Iraqi Islamic State fighters have roots dating back to al Qaeda's campaign of car and suicide bombs that exploded by the dozens each day and succeeded in fuelling a sectarian bloodbath in Iraq, a major oil producer and key U.S. ally.
When a U.S.-funded tribal initiative crushed al-Qaeda, the hardcore regrouped in the desert between Iraq and Syria. They reappeared with a new jihadist brand that took the world by surprise: Islamic State.
Shortly after its lighting sweep through Mosul, the group outdid al Qaeda's brutality, carrying out mass beheadings and executions as it imposed its ultra-hardline ideology.
Unlike al-Qaeda, it seized a third of Iraqi territory, gaining knowledge of land that could come in handy as it hits back at Iraqi security forces.
SADDAM'S INTELLIGENCE AGENTS
Former Iraq intelligence officers who served under Saddam Hussein joined forces with Islamic State in an alliance of convenience. These shrewd military strategists from his Baath Party are expected to be the new generation of Islamic State leaders, Talabany and other security officials said.
Instead of trying to create a caliphate, a concept which attracted recruits from disaffected fellow Sunni Muslims, Islamic State leaders will focus on far less predictable guerrilla warfare, Iraqi and Kurdish security officials said.
Iraqi forces have come a long way since they collapsed in the face of the Islamic State advance in 2014, throwing down their weapons and removing their military uniforms in panic.
They fought for nearly nine months to seize Mosul, with steady help from U.S.-led airstrikes that flattened entire neighbourhoods.
The key question is whether an army that is far more comfortable with conventional warfare can take on an insurgency with sleeper cells and small units of militants who pop out of deserts and mountains, carry out attacks and melt away.
“They’ll try to hide with the population. Their cells will get smaller – instead of companies and platoons, they’ll go to squads and cells, much smaller elements hiding in the population," Lieutenant-General Steve Townsend, commander of the U.S.-led coalition, told reporters.
“Our Iraqi security force partners will have to engage in counter-insurgency style operations at some point and we’re already making efforts now to start shaping their training towards that next ISIS tactic."
History suggests training may not be enough.
The United States spent $25 billion (19.19 billion pounds) on the Iraqi military during the American occupation that toppled Saddam Hussein in 2003 and triggered an insurgency that included al Qaeda.
That did not prepare the army for the long-haired Islamic State militants who sped into Mosul in pickup trucks with weapons stolen from retreating Iraqi troops.
Iraqi forces can certainly point to successes in Mosul and the cities of Falluja and Ramadi in Anbar province, once held by Islamic State.
But local officials say the cities remain vulnerable to attacks from the vast desert nearby mastered by militants.
"Security operations will be useless unless security forces control the desert," said Anbar official Emad Dulaimi, adding that the desert had become a safe haven for Islamic State.
"It is not present as an organisation in cities but it carries out attacks by individuals. Car bombs. Suicide bombers. People fear Islamic State will come back. There are attacks every day."
Tareq Youssef al-Asal, leader of a tribal force, shares those concerns and complains of what he says is a lack of a coordination among numerous local security forces.
"In the end these leaderships have no experience fighting in the desert," he said.
Some ordinary citizens still do not feel safe despite the Iraqi army's improved performance.
Anbar resident Ahmed al-Issawy does not plan on re-opening his restaurant anytime soon. He is afraid it will be destroyed the same way it was in clashes between security forces and Islamic State in 2014.
"I am afraid there could be an attack at any second," he said.
Islamic State has not wasted any time in implementing its new strategy despite a major loss in Mosul.
About 30 militants armed with machine guns and mortars crossed the Tigris river in wooden boats, attacked the village of Imam Gharbi, some 70 km (44 miles) south of Mosul in early July and then pulled out, according to security officials.
"The notion of a caliphate is gone. The dream is gone. They will revert back to their old tactics of hit and run attacks," said senior Kurdish official and former Iraqi foreign minister Hoshiyar Zebari. "The hardcore will keep fighting."