Now that Iran has struck a deal with the P5+1 negotiators over its nuclear ambitions, Tehran is turning its attention to brokering a lasting peace in Syria—not because Iran’s support for the Syrian government is helping Syrian President Bashar al-Assad but because recent developments have forced Iran to reconsider its role in the conflict. What began as indefinite support for the Syrian regime has turned into the propping up of Assad so that Tehran can ultimately use him as a bargaining chip in international negotiations for Syria’s future.

Iran’s change in Syrian strategy is the painful result of Tehran’s miscalculations years ago about what the nation’s conflict would become—what began as an uprising turned, to the surprise of Iranian leaders, into a civil war. Just as Iran’s archrival Saudi Arabia calculated, Iran thought that it could steer the Syrian uprising for its own benefit. Saudi Arabia attempted to cultivate Syrian allies who could topple the Assad regime quickly, whereas Iran backed the regime, believing that it could crush what was then a nonviolent uprising.

Assad’s Iranian-supported crackdown on protesters in 2011 pushed the uprising to take a violent turn, leading Qatar, Saudi Arabia, and Turkey to seek out militant proxies in Syria to both counter the regime and retain their national interests. The Iranian-backed Syrian army took significant hits from moderate armed Syrian opposition supported by the United States as well as from the myriad jihadist groups supported by the Gulf states and Turkey, forcing Iran to reconsider its support for the Assad regime as it became less stable throughout the ensuing months.

In 2012, Iran summoned Hezbollah—which uses Syria to transport Iranian-supplied weapons into Lebanon—to fight on behalf of the Assad regime in Syria. First, veteran military advisers from Hezbollah were sent to guide the Syrian army, but as fighting intensified the group began to send in commanders and troops directly. Iran also sent its own Revolutionary Guard advisers and created local Syrian militias (the National Defense Forces) to help the Syrian army.

Although this boost succeeded in propping up Assad for a while, by 2013 it was matched by increased support for jihadist groups, not only from Gulf countries but also from various private Gulf donors, who saw an opportunity to create their own militias within Syria that could ultimately provide them with increased political leverage over regimes in their own countries. Hezbollah and Iran’s winning streak began to fade in 2014 as these groups pumped more human and material resources into the Syrian conflict. What had begun as a short-term engagement was turning into an existential battle for Hezbollah, and, therefore, for Iran’s influence in the Levant.

The rise of the Islamic State in Syria (also called ISIS) and its subsequent advance in Iraq created another headache for Iran. In the beginning, small Sunni jihadist groups in Syria were beneficial for Iran’s strategy because they served as proof for Tehran that Assad was not quelling a peaceful uprising but, rather, protecting Syria against the threat of violent extremism. But when the jihadist groups morphed into military outfits that were dominated by ISIS while the group also advanced rapidly through Iraq, Iran found itself facing a potential existential threat that was creeping toward its borders.

The international sanctions targeting Iran due to its nuclear enrichment program had already weakened the nation’s economy, and its support for Hezbollah’s activities in Lebanon and Syria created an additional financial strain. With Iraq falling into further disarray, Tehran had to dedicate more resources to protect its interests across the border. Iran came under further strain as it started sponsoring Shia militias in Iraq to fight ISIS while also summoning Hezbollah and its elite Revolutionary Guards to stem the tide of takfiri jihadism, the term that Iranian Supreme Leader Ayatollah Ali Khamenei used for Sunni fighters in Syria. Iran found itself fighting two massive battles at once, and it needed to make a decision about how to best conserve its resources. As Iran is more invested in Iraq than in Syria, Tehran chose to prioritize Iraq.

Meanwhile, Saudi Arabia grew tired of Iran’s military actions in the Arab world, as well as with Western nations for their lack of strategies for ending the Syrian conflict. Capitalizing on similar frustrations by Qatar and Turkey, in the first half of 2015 Riyadh masterminded the creation of new moderate and Islamist rebel coalitions in an effort to increase pressure on the Assad regime. As the coalitions received a tangible boost in the volume and quality of their equipment, as well as improved coordination with now allied sponsors, by March they had begun to deal Assad and Hezbollah more frequent and damaging losses. Reports indicate that Hezbollah has lost a quarter of its elite troops in the Syrian war, and the Syrian army has been reduced to just half its pre-2011 number of soldiers. Both increased their reliance on inexperienced fighters and mercenaries drawn from outside countries, such as Afghanistan, to fill the gaps.

As a result of these losses, Assad has changed his own strategy—but the shift has not been toward Iran’s interests. Instead of trying to maintain a Syrian army presence in almost all of the nation’s governorates, the regime has now retreated from numerous areas that were taken over by rebels or ISIS. Assad’s forces now concentrate instead on maintaining stronghold areas in Damascus and on the nation’s western coast. If ISIS manages to eventually overwhelm other groups in Syria, Assad could appeal to the international community that the world has but two options: his regime or ISIS’ caliphate. The stronger the terrorist group becomes in Syria, however, the more it will continue to threaten Iran’s interests in Iraq.

Iran has realized that betting on Assad to win the war means betting on a losing horse. Tehran has therefore responded by decreasing its aid to him—keeping the regime alive to protect its interests in Syria with the smallest amount of resources necessary. Iran’s main interest today is maintaining Hezbollah’s access to weapons through Syria, not keeping Assad in power, and it is looking for a political solution to the conflict that would retain Hezbollah’s current privileges. Tehran therefore refuses to send troops to Syria, and has reactivated backchannel talks with Saudi Arabia to express its interest in a grand bargain on their respective roles in the Middle East that would not only involve Syria but would include proxy battlegrounds, such as Yemen. The nuclear agreement has accelerated Iran’s pursuit of an end to the Syrian conflict, which has in turn exposed Iran’s weakening relationship with Assad. For example, after Assad’s cousin Suleiman al-Assad killed a Syrian army officer in a traffic dispute in August 2015, Suleiman sought refuge with Hezbollah in Lebanon rather than protection by the Syrian regime.    

Although the shape of a future grand bargain on Syria is still undetermined, it would likely include Iran’s acceptance of the creation of a transitional government in Syria that retains elements from the current regime and also safeguards Hezbollah’s current privileges. Iran is still publicly insisting that Assad himself must play a role in this deal, that elections must eventually take place, and that his presidency must therefore come to a natural end. This gesture is meant to placate Assad and ensure that he retains some value in Syria; dropping him from negotiations now would hand Iran a complete loss within Syria. Instead, holding on to Assad while bringing the Syrian conflict to an end means that Iran can offer him as a small sacrifice—one that gives Tehran assurances that Assad’s successor will have the blessing of the West, as well as of regional actors such as Saudi Arabia, while also maintaining Iran’s interests in the Levant.

This article originally appeared on Foreign Affairs.