The U.S. government has killed one of its most significant foreign adversaries in decades, with the death of Iranian Maj. Gen. Qasem Soleimani.

But the U.S. relationship with Soleimani, like many in the Middle East, has been a complicated one, and he wasn’t always on the opposite side. They even worked together.

As the New Yorker’s Dexter Filkins wrote in 2013, there was a time when there seemed to be hope for something amounting to an alliance between the United States and Iran in Afghanistan after Sept. 11, 2001. And Soleimani, who led Iran’s Quds Force, which is charged with carrying out operations beyond the country’s borders, was the point person:

In the chaotic days after the attacks of September 11th, Ryan Crocker, then a senior State Department official, flew discreetly to Geneva to meet a group of Iranian diplomats. “I’d fly out on a Friday and then back on Sunday, so nobody in the office knew where I’d been,” Crocker told me. “We’d stay up all night in those meetings.” It seemed clear to Crocker that the Iranians were answering to Suleimani, whom they referred to as “Haji Qassem,” and that they were eager to help the United States destroy their mutual enemy, the Taliban. Although the United States and Iran broke off diplomatic relations in 1980, after American diplomats in Tehran were taken hostage, Crocker wasn’t surprised to find that Suleimani was flexible. “You don’t live through eight years of brutal war without being pretty pragmatic,” he said. Sometimes Suleimani passed messages to Crocker, but he avoided putting anything in writing. “Haji Qassem’s way too smart for that,” Crocker said. “He’s not going to leave paper trails for the Americans.”

Crocker described sharing information with the Iranians, including getting a map detailing the locations of the Taliban and giving Iran the location of an al-Qaeda facilitator, whom Iran soon detained. Crocker said the negotiator he was working with told him, “Haji Qassem is very pleased with our cooperation.”

But things soon changed. Despite that cooperation, in January 2002, President George W. Bush lumped Iran in with Iraq and North Korea in his famous “axis of evil” speech. Crocker said he was blindsided, and that it effectively ended things:

He saw the negotiator the next day at the U.N. compound in Kabul, and he was furious. “You completely damaged me,” Crocker recalled him saying. “Suleimani is in a tearing rage. He feels compromised.” The negotiator told Crocker that, at great political risk, Suleimani had been contemplating a complete reevaluation of the United States, saying, “Maybe it’s time to rethink our relationship with the Americans.” The Axis of Evil speech brought the meetings to an end. Reformers inside the government, who had advocated a rapprochement with the United States, were put on the defensive. Recalling that time, Crocker shook his head. “We were just that close,” he said. “One word in one speech changed history.”

Eventually, when the United States went to war in Iraq in 2003, the relationship turned even more adversarial. As the Atlantic Council’s Iran expert Barbara Slavin wrote in the New York Times on Friday, suddenly the incentives were recalibrated:

General Suleimani became an enemy after the United States invaded Iran’s other neighbor, Iraq, rebuffed an Iranian offer for wide-ranging negotiations and gave protection to the Mujahedeen Khalq, a ruthless opponent of Iran that had been nurtured by Saddam Hussein. Of course, in toppling Mr. Hussein, the United States opened Iraq to deep penetration by Iraqis sheltered and groomed by Iran during the 1980-88 Iran-Iraq war. Iran became the pivotal player in Baghdad, taking advantage of free elections, which predictably brought to power successive governments sympathetic to Tehran.

Crocker in a 2013 Q&A reflected on another moment when Soleimani indirectly reached out — in April 2008, when Iraqi Prime Minister Nouri al-Maliki launched an offensive in the south of that country.

“It was pretty scary for a few days, but [Maliki] prevailed, with a lot of our help. But he prevailed,” Crocker said. “That rattled a lot of Iranians, including Soleimani, who asked for a meeting with [Iraqi President] Jalal Talabani and Vice President Adel Abdul-Mahdi.

“They came back, asked [Gen.] David Petraeus and I to dinner and said: Soleimani has a message for you. We seek the same goals — a strong and stable Iraqi government. It was so much fluff. But the fact that he did it at all suggests that Maliki’s offensive had taken him off-balance, and he wasn’t sure what might be coming next, how we might be emboldened by this.”

Filkins recounted how, around the same time, Soleimani was reaching out to U.S. leaders with varied messages, including a text message for Petraeus:

One of the first came in early 2008, when the Iraqi President, Jalal Talabani, handed a cellphone with a text message to General David Petraeus, who had taken over the year before as the commander of American forces. “Dear General Petraeus,” the text read, “you should know that I, Qassem Suleimani, control the policy for Iran with respect to Iraq, Lebanon, Gaza and Afghanistan. And indeed, the ambassador in Baghdad is a Quds Force member. The individual who’s going to replace him is a Quds Force member.” After the five American soldiers were killed in Karbala, Suleimani sent a message to the American Ambassador. “I swear on the grave of Khomeini I haven’t authorized a bullet against the U.S.,” Suleimani said. None of the Americans believed him.

Filkins added that, while Petraeus called Soleimani “truly evil,” “at times the two men were all but negotiating.” Diplomatic cables leaked by WikiLeaks showed Petraeus communicating through Iraqi leaders with Soleimani. At one point, he helped secure a cease-fire between radical Shiite cleric Moqtada al-Sadr’s militia and the U.S.-backed Iraqi government.

The interests were somewhat aligned again with the rise in Iraq and Syria of the Islamic State, also known as ISIS, which amounted to a common enemy for Iran and the United States. Iranian-backed militia led by Soleimani were involved in pushing it back in Iraq, in part because the extremist Sunni Muslim group presented a potential threat to Shiite Iran and in part because Iran sought to maintain influence over Iraq’s future.

But just across the border, Iran and Soleimani were allied with Syrian President Bashar al-Assad in that country’s civil war. Fast-forward to today, and Iran and the United States are increasingly at odds after the Trump administration’s pullout of the 2015 Iran nuclear deal and accompanying sanctions. After Soleimani’s killing, Secretary of State Mike Pompeo said he was spearheading “imminent” plots to attack Americans and U.S. interests in the region.