Iranians celebrated in the streets of their cities this weekend in the aftermath of presidential elections out of relief that the most capable candidate on offer won.

Incumbent President Hassan Rouhani’s solid victory over a younger and more hardline cleric, Ebrahim Raisi, was a triumph of competence over ideology and of openness over isolationism.

Rouhani, a pragmatic regime survivor, has governed with a team of technocrats who negotiated a complex nuclear agreement with the international community led by Iran’s long-time adversary, the United States. Thanks to the Joint Comprehensive Plan of Action (JCPOA), growth was restored to the Iranian economy after years of free-fall under punishing sanctions.

Raisi, on the other hand, surrounded himself with advisers to Rouhani’s predecessor, Mahmoud Ahmadinejad. Apart from stealing the 2009 election, Ahmadinejad is chiefly remembered for accelerating the nuclear program, embarrassing Iranians with oddball speeches and implementing economic policies that squandered record oil revenues, collapsed the currency and led to high inflation.

Ahmadinejad, a non-cleric, at least had some executive experience when he was elected president. Raisi has been primarily a high-level bureaucrat in the judiciary, among Iran’s most repressive institutions. He is implicated in the summary execution of thousands of political prisoners in 1988, at the end of the Iran-Iraq war.

A protégé of Iran’s paramount leader, Ayatollah Ali Khamenei, Raisi was named by Khamenei last year to head Iran’s biggest religious foundation, the shrine of Imam Reza, in Mashhad. That proximity to immense wealth made Raisi an unlikely champion for Ahmadinejad-style populism. Raisi’s talk of a “resistance economy” also suggested a return to isolationism instead of the greater integration that Iran needs to achieve durable economic progress.

In the aftermath of the vote, one Iranian tweeted that he had voted for Rouhani “so things don’t get any worse.”

As that qualified endorsement suggests, Rouhani faces plenty of obstacles in his second term – a time when Iranian presidents have traditionally been weaker than in their first incarnation. There are numerous uncertainties, not least how long the current Supreme Leader will live and who might replace him.

Especially worrisome is the hostile attitude of the Trump administration. While it has abided by the minimum requirements of the nuclear deal, its apparent decision to side wholeheartedly with Saudi Arabia on regional matters risks inflaming rather than resolving the sectarian conflicts tearing apart the Middle East.

Speaking in Riyadh on Sunday in a major address to the Muslim world, Trump failed to even acknowledge Rouhani’s win and what it represented in terms of Iranian aspirations. Instead, Trump called for isolating Iran until “the Iranian regime is willing to be a partner for peace” and said people should “pray for the day when the Iranian people have the just and righteous government they deserve” — a remark undercut by the fact that he was speaking in a hereditary monarchy with even more limited rights of political participation and social freedom.

Given the hostile US attitude, it is not clear whether Rouhani’s team will have more success enticing foreign investment or whether major European banks will have more confidence to restore relationships with their Iranian counterparts. Had Raisi won, however, the chances for progress on those fronts would certainly have been even smaller.

Facing an antagonistic Washington, there is still much Rouhani can do to heal Iran’s internal divisions — if the Supreme Leader, the Revolutionary Guards and myriad intelligence and security bodies allow. In a positive sign, Rouhani referred in his victory speech and in comments made during the campaign to former President Mohammad Khatami, a reformist who governed from 1997-2005 and who remains Iran’s most popular politician. The regime had forbidden even a mention of Khatami’s name in public after the 2009 election protests. But Khatami’s enthusiastic endorsement of Rouhani on messaging aps this year and in 2013 were crucial to his victories as well as to the success of pragmatic candidates in last year’s parliamentary elections.

Rouhani could also push to end the house arrest of Mir Hossein Mousavi and Mehdi Karroubi, two former presidential candidates who have been prisoners in their homes since 2011 for protesting the rigged 2009 re-election of Ahmadinejad. Rouhani could seek to free jailed journalists, lawyers and civil society advocates as well as dual nationals, such as Iranian-Americans Siamak Namazi and his 81-year-old father, Baquer, who are languishing in Evin prison on bogus charges. Rouhani could follow the pattern of post-Mao China and encourage members of the large Iranian diaspora to return and bring their expertise and financial resources with them.

During the campaign, Rouhani spoke of building on the JCPOA by negotiating an end to non-nuclear sanctions that have impeded Iran’s economic recovery and left the country plagued by high unemployment. That would require a willingness in Tehran to discuss Iran’s support for groups that the United States regards as terrorist such as Hezbollah and Hamas, as well as possible limitations on Iran’s ballistic missile development. It would require a US willingness to engage as well.

Rouhani could also reach out more convincingly to the Saudis. Instead of the “Arab NATO” the Saudis have been trumpeting, Iran’s Sunni rivals should consider a more inclusive regional security architecture that could resolve conflicts instead of escalating them.

While neither Trump nor Saudi King Salman congratulated Rouhani in the aftermath of the election, scores of other foreign leaders did.

Federica Mogherini, the High Representative of the European Union for Foreign Affairs and Security Policy, tweeted that “Iranians took passionately part to political life of their country.” The EU, Mogherini said, was “ready to continue work for full JCPOA implementation, bilat engagement, regional peace, and meet expectations of all people in #Iran.”

Most of the world wants Rouhani to succeed in preserving the nuclear agreement and in turning down the temperature in a boiling Middle East.

It would be wise for the Trump administration to test Iran’s willingness to re-engage and not uncritically embrace the views of the Saudis and their Sunni allies. In a Middle East mostly led by monarchs and dictators – or in the throes of civil war – Iranians deserve some credit for what they have been able to demonstrate at the ballot box. Once again, they have shown their ability to make lemonade out of lemons.

Barbara Slavin is Acting Director of the Future of Iran Initiative at the Atlantic Council.