Akbar Hashemi Rafsanjani, a former Iranian president whose support for social freedoms and outreach to the United States made him a powerful ally of moderates despite allegations of corruption and authoritarianism, died Sunday.

The cause was a heart attack, state news media reported.

Iran declared three days of mourning for one of its most significant political figures, whose backing helped moderate President Hassan Rouhani win election in 2013, setting the Islamic Republic on a path to ending its disputed nuclear program and easing its isolation from the West.

A former aide to Iran’s revolutionary supreme leader, Ayatollah Ruhollah Khomeini, Rafsanjani’s pragmatic views on foreign economic investment and cultural issues set him apart from much of the theocracy’s conservative establishment.

Although his influence had waned in the two decades since his presidency, he was a political survivor and behind-the-scenes ally of moderate and reformist forces agitating for looser political and social controls. He called for the release of dissidents and sharply criticized the anti-West former president, Mahmoud Ahmadinejad, who left office in 2013.

That year, after Iran’s conservative election body barred Rafsanjani from seeking the presidency, he threw his support behind Rouhani, seen as a much friendlier face to the West than Ahmadinejad. After Rouhani’s election, Rafsanjani was reported to have said, “Now I can die peacefully.”

Rafsanjani’s death, five months before Rouhani is due to face re-election, was a blow to moderates who are facing growing criticism for Iran’s continuing economic struggles.

Under Rouhani, Iran signed an agreement with world powers to shelve its nuclear program in exchange for the lifting of many Western economic sanctions. The deal has not brought significant foreign investment or reduced unemployment, strengthening hard-liners who opposed accommodations with the West and possibly threatening Rouhani’s chances at a second term.

Born to a pistachio-farming family in southeastern Iran, Rafsanjani studied Shiite Muslim theology at the holy city of Qom with Khomeini, who led the 1979 revolution that toppled a West-friendly monarchy.

Khomeini appointed Rafsanjani a commander of Iran’s armed forces toward the end of the devastating Iran-Iraq war, in which an estimated 1 million died from 1980 to 1988.

Khomenei died in 1989, and one month later Rafsanjani took office as president, leading a postwar reconstruction and beginning a period of economic and cultural liberalization. Arts and film began to flourish, men and women could socialize more openly, and women felt more comfortable wearing lipstick or allowing their hair to peek out from under their mandatory head scarves in public.

At the same time, political space remained tightly controlled and Rafsanjani tolerated no challenges to his authority. He stayed publicly silent – and many critics accused him of involvement – when opposition leaders were targeted for assassinations overseas or died mysteriously in police custody. The deaths were never investigated.

He also amassed tremendous personal wealth through a business empire that included pistachios and construction, leading many Iranians to view him as corrupt.

Yet in the context of Iranian politics, he was a relative moderate, attempting outreach to Saudi Arabia and the “Great Satan” – the United States.

In the 1980s, as parliament speaker, he was a go-between for Reagan administration officials during the Iran-contra affair. As president, in 1995, he offered Oklahoma-based Conoco the first Iranian oil contract with a foreign company since the revolution, but the deal was scuttled by then-President Bill Clinton.

After his presidency, he was one of the leaders of weekly prayers at Tehran University, a prominent space to deliver religious and political messages. But he angered the establishment when he backed the Green Movement protesting alleged vote rigging in Ahmadinejad’s 2009 re-election, which prompted a harsh crackdown against opposition politicians and their supporters.

Rafsanjani lost his place among prayer leaders but kept his position as head of the Expediency Council, an advisory body to Supreme Leader Ayatollah Ali Khamenei. With Khamenei in failing health, Rafsanjani was expected to play a prominent role in determining his successor.

The more immediate challenge is to Iran’s moderates and reformists, a fractious coalition that united behind Rouhani in 2013 and in parliamentary elections last year. Without Rafsanjani’s support, reformists worry their political gains will erode.

Rafsanjani, who is survived by his wife, Effat Marashi, and five children, died at 6 p.m. at a hospital north of Tehran, news agencies reported.

His body was brought to the town of Jamaran, Khomeini’s former home, where dignitaries arrived to pay their respects. They included Foreign Minister Javad Zarif and former President Mohammad Khatami, Rafsanjani’s successor.