Peter Bergen is a CNN analyst, a vice president at New America and a professor of practice at Arizona State University. He is the author of " United States of Jihad: Investigating America's Homegrown Terrorists ."

(CNN) The tidal wave of tens of thousands of "foreign fighters" that once flocked from around the Muslim world and beyond to ISIS' black banners has slowed to a trickle. Estimates cited by The Washington Post suggest that the flow of foreign recruits to ISIS had dropped from a high of 2,000 a month to 50 a month by last fall.

Few foreign militants want to join the losing team.

On Monday, Iraqi Prime Minister Haider al-Abadi declared the defeat of ISIS in Mosul, the second-largest city in Iraq and the place where three years ago the terror group first announced its self-styled caliphate.

The loss of its Iraqi capital as well of much of its territory in Iraq and Syria dramatically undercuts ISIS' claim that it is the caliphate because the caliphate has historically been both a substantial geographic entity such, as the Ottoman Empire, as well as a theological construct.

While the victory over ISIS at Mosul is certainly to be celebrated and its fighters are now more concerned about simple survival than plotting attacks in the West, it's worth recalling that ISIS continues to hold the Iraqi towns of Tal Afar (population 100,000) and Hawija (population 115,000) and its de facto Syrian capital, Raqqa (population around 200,000).

JUST WATCHED Mosul liberation comes at a price Replay More Videos ... MUST WATCH Mosul liberation comes at a price 03:03

The campaign to liberate Raqqa is now underway, but given the fact that it took around eight months to expel ISIS from Mosul we should expect a long battle for Raqqa.

Also, the one thing that really brought together the fractious sects and ethnic groups of Iraq --- the Kurds, the Shia and most of the Sunnis -- was their shared hatred of ISIS. With ISIS sharply declining in power, the tensions that have long existed in Iraq between these various groups will likely reassert themselves.

Which brings us to the bigger picture: ISIS was never the root problem in Iraq -- even though it certainly created great misery among those it lorded over -- but rather the group was the symptom of deeper problems that exist in the Middle East that are unlikely to disappear anytime soon.

ISIS, after all, is a branch of al Qaeda in Iraq, which was founded more than a decade ago. After suffering a near total defeat by US forces in Iraq between 2007 and 2010, al Qaeda regrouped in neighboring Syria as that country descended into a civil war beginning in 2011.

Al Qaeda in Iraq subsequently rebranded itself as ISIS.

ISIS emerged in Syria because it was seen as one of the few Sunni groups truly capable of standing up to the brutal Shia Alawite regime of Syrian dictator Bashar al-Assad.

Similarly, ISIS did well in Iraq when it swept across the country in 2014, in part, because many Iraqi Sunnis were fed up with the deeply sectarian Shia government of then-Prime Minister Nuri al-Maliki.

The deep divisions between many Sunnis and Shia in both Iraq and Syria and also in countries such as Yemen, where Sunni Saudi Arabia and Shia Iran are fighting a proxy war, are likely to continue for many years. These are the conditions that will surely set the stage for the emergence of a son of ISIS (and even a grandson of ISIS).

At the same time, the collapse of governance in Arab countries such as Libya, Yemen and Syria has provided the breeding ground for groups such as ISIS and al Qaeda that thrive in countries where there is a leadership vacuum.

Follow CNN Opinion Join us on Twitter and Facebook

This is also compounded by the post-Arab Spring collapse of many Middle Eastern economies.

In turn, these factors have produced a massive and unprecedented wave of Muslim immigration into Europe. This influx has caused great political turbulence in Europe, enabling the rise of ultranationalist parties from France to Poland.

All of these factors have interacted to produce Sunni jihadists in the Middle East and to create fertile soil in Europe for the ideology of jihadism to take root among alienated, young Muslim men such as the ISIS recruits who carried out the deadly terrorist attacks in Paris, Brussels and Manchester, England, over the past two years.

That takes us to the unhappy conclusion that the war against the terrorists is far from over.