From the first time I met Ali Abdullah Saleh, at his sprawling palace in the hills above Aden, I could tell he was a force to be reckoned with—but one that would never be fully understood. Saleh famously likened leading Yemen to “dancing on the heads of snakes,” and indeed, his career resembled an intricate ballet in which it was never quite clear where the dancer would put his feet next. Outwardly reserved, inwardly shrewd, he worked all the angles: Saudis and Iranians, Sunni militants and Zaydi Houthis, the United States and Russia, the United Arab Emirates and the Muslim Brotherhood. Ultimately, it was this flip-flopping that sealed his fate.

In the 1990s, after using Islamic militants to win Yemen’s civil war and unifying the country under his rule, Saleh turned a blind eye to their activities, even allowing their sympathizers to work in his intelligence services. Shortly before the USS Cole was bombed by al-Qaeda in Aden’s harbor in 2000, Saleh gave a speech supporting jihad against Israel by its neighbors and railing against the United States for its support of the Jewish state. When U.S. agents, myself included, arrived to investigate the attack, Saleh promised cooperation, only for elements of his intelligence services to stymie our progress at every turn.

After 9/11, Saleh sided with America in the War on Terror. But he continued the dance. Time after time, I met with him to discuss the mysterious “escape” of yet another al-Qaeda convict in Yemeni custody; Saleh would demand more American money and support in order to catch the fugitive. Sometimes, having received what he needed, he would decide to pardon them, further infuriating the United States. But Saleh knew he was an indispensable partner, and the aid kept on flowing.

While in office, Saleh fought six wars, with Saudi support, against the Iran-backed Houthi militia. Then, in 2015, three years after he was deposed, Saleh came roaring back—this time in alliance with the Houthis. His forces helped the militants grab much of western Yemen, including the capital, Sana’a. Even then, there were indications that he was still playing the other side, for his son and nephew (both important figures in his government) remained in Abu Dhabi, a key Saudi ally in the Yemen conflict.

Just last week, Saleh switched sides yet again, repudiating his deal with the Houthis and promising to turn a “new page” with Saudi Arabia and the UAE. The Saudi coalition immediately reciprocated with a fierce bombing campaign against Houthi units surrounding Sana’a. But Saleh had finally miscalculated: Forces once loyal to him personally now felt more allegiance to the Houthi cause. All indications are that the Houthis fully expected Saleh to turn on them, and had long prepared for that eventuality; their rallying cry as they moved into Sana’a was “Vengeance for Hussein al-Houthi”—a reference to their former leader, killed on Saleh’s orders 13 years ago. The dancer stumbled, and the snake gobbled him up.

Clearly, Ali Abdullah Saleh was no saint. In the poorest country in the Middle East, corruption made him one of the richest men in the world. Saleh never made a play that didn’t benefit him in some way. But he had his virtues, too. After his fall from power in 2012, he could have sought asylum in any number of world capitals, yet he chose to return home. “I will die in Yemen,” he said, and he remained true to his word—demonstrating a level of commitment to his country and its people. For all his faults, Saleh’s new alignment with the Saudis represented Yemen’s last, best hope of breaking the bloody stalemate in its civil war. If he had been able to hold the capital, perhaps the Kingdom might have been able to declare a face-saving win and start withdrawing from the quagmire it created.

That the new alliance unraveled in a matter of days says much about contemporary Yemen and the broader Middle East. In the face of growing Iranian influence, Saudi Arabia has set itself up as a Sunni counterweight; in the process, it has sectarianized conflicts across the region. Where once it was possible for figures like Saleh to switch sides repeatedly for political reasons, that becomes much harder when those supporters have been encouraged to believe they are fighting a religious war. Perpetuating a war that has caused heavy civilian casualties, famine, and cholera, is not a policy likely to win the Saudis the support of the millions whose lives have been destroyed.

Sectarianism, proxy wars, and the Arab Spring, have each fundamentally altered the way power works in Yemen and across the region—albeit not in ways the instigators of those forces wanted or expected. Saleh failed to understand the new reality, and it cost him his life.