SANAA Ali Abdullah Saleh, who on Wednesday surrendered power after nearly a year of bloodstained protests to end his rule, once compared running Yemen to "dancing on the heads of snakes."
His dancing days appeared to end as he signed a deal to hand over power to his deputy Abd-Rabbu Mansour Hadi, while keeping the title of president until a new election within 90 days, according to a deal struck with a group of opposition parties.
It appeared to finally divorce his own fate from that of the poor, unruly and fractious nation he presided over through civil war, militant campaigns and tribal feuds for 33 years. But it may not provide Yemen with a roadmap out of misery and chaos.
The bombing of his compound in June, an apparent assassination attempt, covered him with severe burns and drove him to recover in Saudi Arabia, one of the resource-rich neighbors which had tried in vain to ease him from office.
Saleh, ever nimble, broke three deals to transfer power before the bid on his life, shrugged off U.S. and Saudi pressure to stay in exile and made good on vows to return home in September.
But international pressure redoubled in October, when the U.N. Security Council adopted a resolution demanding he sign the handover plan brokered by Gulf neighbors. France and Britain hinted at European Union sanctions on him.
Yemen has seen steadily intensifying street protests against Saleh's rule since January, culminating in clashes between forces of the Hashed tribal group and troops loyal to Saleh in the capital and elsewhere.
The United States has talked openly of its concern about who might succeed Saleh, an ally in the fight against al Qaeda in the Arabian Peninsula, a Yemen-based wing of the militant group.
Only recently, Saleh's rule seemed all but unshakeable. Last year, supporters pushed for constitutional changes to allow him unlimited five-year terms as president. Speculation was high that he was grooming one of his sons as a possible successor.
But popular uprisings that toppled Tunisian President Zine al-Abidine Ben Ali and Egypt's Hosni Mubarak resonated in Yemen, drawing tens of thousands to daily protests in Sanaa and in Taiz to the south, threatening his apparent dynastic ambitions.
Saleh began to offer concessions.
First Saleh said he would not stand for re-election in 2013 and dismissed the idea of his son succeeding him, then offered a referendum on a new constitution by the end of the year and a shift to a truly democratic "parliamentary system."
But after the death of 52 protesters, mostly by sniper fire, in March, a string of generals, tribal leaders, diplomats and ministers either resigned or declared their public allegiance to the protesters.
They included key figures in the al-Ahmar and Sanhan tribes, kinsmen who Saleh placed in key military and other positions.
He tied his fate to theirs when confronted with the power transfer deal brokered by the six-member Gulf Cooperation Council, which he also shunned while complaining that one of its architects, Qatar, was agitating against him through its media.
Saleh became the ruler of North Yemen in 1978 at a time when the south was a separate, communist state, and has led the unified country since the two states merged in 1990.
Opponents often complained that Saleh failed to meet the basic needs of Yemen's people, two-thirds of whom live on less than $2 per day. Oil wealth is dwindling and water is running out, though liquefied natural gas exports began in 2009.
Yet Saleh managed to keep Western and Arab powers on side.
After the September 11, 2001 attacks against U.S. cities, Washington became more aware of Yemen as it was a source of foot soldiers for Osama bin Laden's al Qaeda network. Bin Laden himself, though born in Saudi Arabia, originated from Yemen's Hadramaut region.
Saleh cooperated with U.S. authorities, and the CIA carried out a successful drone attack against a wanted figure. But by 2007, militants had regrouped in Yemen and in 2008 they announced that their Saudi and Yemeni wings had united under the banner of al Qaeda in the Arabian Peninsula.
From 2009, the resurgent group made ever bolder attempts to stage attacks on Saudi and U.S. targets beyond Yemeni soil, as well as targeting foreign tourists at home. At the same time, northern Shi'ites rebelled against Saleh's rule and southerners, feeling marginalised, began a new separatist drive.
Saudi Arabia, the United States and other allies responded by stepping up financial support to bolster Saleh's rule.
If Saleh has been a determined survivor, he has also been a charismatic and often popular ruler who understood well the workings of Yemeni society.
Born in 1942 near Sanaa, Saleh had only limited education before joining the military as a non-commissioned officer.
His first break came when President Ahmed al-Ghashmi, who came from the same Hashed tribe as Saleh, appointed him military governor of Taiz, North Yemen's second city. When Ghashmi was killed by a bomb in 1978, Saleh replaced him.
In 1990, an array of domestic and regional circumstances propelled North Yemen under Saleh and the socialist South Yemen state into a unification that Saudi Arabia at first opposed.
He angered Riyadh by staying close to Saddam Hussein during the 1990-1991 Iraqi occupation of Kuwait, leading to the expulsion of up to 1 million Yemenis from Saudi Arabia. Before the crisis, Kuwait had given Yemen financial aid.
But Saleh then won plaudits from Western powers for carrying out economic reforms drawn up by the International Monetary Fund and World Bank, and made efforts to attract foreign investors.
He swept to victory when southerners tried to secede from united Yemen in 1994 and drew closer to Saudi Arabia, which he allowed to spread its radical Wahhabi form of Sunni Islam.
(Writing by Sami Aboudi and Isabel Coles; Editing by Jon Hemming)