As the fireball exploded on Iran's western border and fighters from the Islamic State of Iraq and the Levant (ISIL) swept through northern Iraq at a startling pace, a sense of urgency shrouded Tehran.
On June 12, Iran's Supreme National Security Council called a snap meeting . Inside sat some of Iran's most powerful men; President Hassan Rouhani, Supreme National Security Council Secretary and Representative of the Supreme Leader Rear Admiral Ali Shamkhani, and Intelligence Minister Mahmoud Alavi.
The men are responsible for deciding the most sensitive strategy for the country of 75 million people. Strategy played out beyond its borders.
The minutiae of what happened inside the meeting are not clear, but Iraq, sources in Tehran to Al Jazeera, was the agenda. The Kurds had taken Kirkuk ; ISIL was gaining territory; the Iraqi army was in disarray, deserting and leaving behind a vacuum in parts of the north.
The Iranians had put their troops - and air force - on full alert and sent reinforcements to the border. Amid all this, Iran's ally, Iraqi Prime Minister Nouri al-Maliki, stuck in Baghdad, was quickly losing control.
Iran will bargain the future of Maliki because Iran wants something back. So if the Americans say Maliki must go, then Iran will say 'What do we get in return?' But for now, Iran would never say that publicly.
What is at stake for the Iranians is enormous, say analysts. ISIL presents a direct threat to its territory, power, influence, and indeed, security. So what the men at the meeting had to decide was how Iran was to going to get a hold of these things - and if they could get anything else out of the raging mess across its border.
One reason why Iraq is so important is, "to keep the conflict outside of its country, to an outside territory, so it can defeat the enemy there", says Peyam Mohseni, Director of Iran Studies at the Harvard Kennedy Center.
One man who knows these things well - an old acquaintance of Maliki's - is Iran's formidable Qods Force commander - the elite unit of the Revolutionary Guard Corps (IRGC). As Maliki floundered, Qasem Soleimani was there to advise him and the Iraqi forces on strategy and on how to keep hold of Baghdad.
Why? As Mohseni puts it: "It's the regional cold war. Iran, when it views the different arenas, Iraq, Syria, it just doesn't think of it as a domestic Syrian or Iraqi thing. It sees them as really an attack on Iran or a proxy war with Iran."
The doctrine of the Qods Force is simple; protect the leader, the revolution and spread its interests. For the past decade, Iraq has been the regional battlefield for this strategy .
According to Fawaz Gerges, pre-eminent Middle East and Iran scholar, it was "easy" for the IRGC to operate in Iraq - especially after the 2003 US-led invasion and occupation: "Iran is the most influential state inside Iraq. They have built critical capital, social capital inside the country. And really what the Iranians tried to do, was translate these ties into resources, which they did after the Americans [left]. It's not surprising, all they did was deepen and thicken their ties inside Iraq, with the dominant social group. Iran's influence goes beyond Maliki and the Shia, this is important, its influence is bottom up."
And these strategies worked to keep Iran in a position of power in Iraq, and says Iranian IRGC specialist Ali Alfoneh, "the Iraqi Shia in suspension and in a constant state of dependency on Tehran".
Q&A with Falah Mustafa, head of the Kurdistan Region's foreign relations
One key element of Iran's Iraqi success formed 34 years ago, in a military base in Khuzestan, near the Iran-Iraq border. The Qods force had risen out of the Ramezan Base and men stationed there had nurtured many of the contacts Iran now needs to fight in Iraq: Men like Brigadier General Iraj Masjedi, who rather proudly in 2011, described Iran as "the most influential stream in Iraq's political issues", and Soleimani himself. From the base, the men built a network of ties within enemy Iraq; from the Shia militia in the south to Kurdish Sunni leaders in the north.
Those ties gave the men the ability to reach out, when Tehran - and even Baghdad - could not.
One contact is Kurdish Prime Minister Nechirvan Barzani. Four days after the SNSC meeting in Tehran, Barzani made an unannounced trip to the Iranian capital to meet with Shamkhani.
The Kurdish seizure of Kirkuk had raised serious questions among the council; and infuriated the Iraqi prime minister.
Barzani went to Tehran, for a simple reason, says Gerges: "To allay fears, of course. Barzani and the Kurds know very well that Iran is a very influential and a powerful player inside Iraq and it's in the interest of the Kurds to keep on good terms with them. The Kurds are not going to give back Kirkuk, it's common-sense."
His visit marked another curiosity in Iran's complex relationship with the Kurds.The Islamic Republic, after its formation, bombed the Kurds, but then later forged an alliance with Jalal Talabani's Patriotic Union of Kurdistan Party (PUK) and Masoud Barzani's Kurdish Democratic Party of Iraq (KDPI).
With the re-emergence of the Kurds, the Iranians see an opportunity, according to analysts. The Kurds are a stronger ally in the fight for Iraq than Maliki - who has lost support almost everywhere - and that finally gives Iran a good reason to ditch him. And reap rewards from the US - that other power trying to avoid the collapse of Iraq.
So a week after his Tehran trip, Barzani did what many had been waiting for; he told Maliki to step down. And he aired his views - very publicly - in an interview with the US' NBC news .
And with that, what Iran - or at least some of the men of the SNSC - want is becoming clearer, "Iran will bargain the future of Maliki," says Gerges, "because Iran wants something back. So if the Americans say Maliki must go, then Iran will say 'What do we get in return?' But for now, Iran would never say that publicly".
Q&A: 'Perhaps the last chance to save Iraq'
Polls taken in Iraq during the last five years also show, by and large, the loathing for Maliki is not limited to his old enemies, nor is it split along sectarian lines.
The Sunni and Shia both dislike him for his sectarianism and accuse Maliki of using these policies to amass power, making casualties of his people.
Even one of the world's most respected and highest Shia authorities, Iraq's Grand Ayatollah Ali Sistani, delivered Maliki a veiled warning on June 20 about this very thing.
It has also created conditions where the Shia militia can now return. These are the networks the IRGC had carefully cultured for so many years - and still exerts influence over.
And this presents a key element going forward. As Mohseni and Alfoneh argue, the IRGC put in the hard work on the ground, supporting the militia and cultivating conditions in which Iran gained the upper hand. Now is a time for working those contacts, and seeing what Iran can get out of it.
Iran's diplomats - as evident in similar comments from Rouhani's deputy Chief of Staff Hamid Abdoutalebi, the president himself and Foreign Minister Mohammad Javad Zarif, now have more bargaining chips and leverage.
As Mohseni puts it: "Many times we see ... that while the Revolutionary Guards may be doing these things, the moderates are taking the position of 'Well, we'll negotiate with them now.'"
And that could mean if Iran correctly navigates the Kurds, Maliki and the militia - it could have leverage with issues it views as more important, including a better nuclear deal.
The only problem that analysts and sources in Iran see, is that the ISIL threat could rage well out of Iran's hands. Borders between Syria and Iraq are already falling and ISIL is establishing its new "state" on Iran's doorstep.
And then, no matter how deep Iran's ties are, no matter how many bargaining chips they have, no matter how smart its diplomats are, Iran's only option left will be to save itself.