THE election of a civil-rights campaigner as Somalia’s new president has engendered a rare wave of hope in a country that has had no proper government since 1991, when its military dictator, Siad Barre, was overthrown in a coup. “It’s the biggest opportunity for the country for a generation,” says a Western diplomat involved in the process. The new head of state, Hassan Sheikh Mohamud, is being lauded as an honest man of peace and pragmatism.

But the dangers and uncertainties of his job were starkly illustrated only two days after his election when he survived an assassination attempt in a hotel where he was meeting Kenya’s foreign minister in Mogadishu, the capital. A suicide-bomber belonging to Somalia’s al-Qaeda-linked Islamist militia, known as the Shabab, blew himself up outside the hotel, killing several people, including a soldier in the force run by the African Union (AU) that has been putting the Shabab on the back foot.

Despite this attack, a steady improvement in security is another cause for hope. In the past few months the Shabab has been pretty well chased out of Mogadishu, though it can still launch sporadic terrorist actions. At the same time Ethiopian and Kenyan troops have been hammering the Shabab from the west and south. An AU force recently captured Merka, a port south of Mogadishu. The country’s second city, the port of Kismayu, the Shabab’s last major stronghold, was bombarded earlier this month by Kenyan troops. If it fell, it would be a big fillip for the new president.

Mr Hassan was not chosen by universal franchise but by a newly established 275-member parliament that was in turn handpicked a month ago by a conclave of clan elders. In a first round of voting, in which 22 candidates competed, Mr Hassan came second to the incumbent, Sharif Sheikh Ahmed, but prevailed over him easily with 190 votes to 79 in a run-off.

Diplomats heaved a sigh of relief at the demise of the free-spending and widely mistrusted Mr Sharif. It is eagerly hoped that the new parliament will be better than its woeful predecessors and will soon choose a plausible government. Previous transitional ones were picked in foreign conference rooms by international fixers—with uniformly bad results.

The challenges facing Mr Hassan are daunting. Despite the Shabab’s retreat, the government’s writ will run little further than the outskirts of the capital, along with a few strategic towns scattered across otherwise dangerous territory. The Shabab still controls swathes of southern and central Somalia. Security in Mogadishu and elsewhere depends on the AU’s troops more than on Somalia’s own forces.

Somali pirates, whose oceanic depredations deter investment and trade on dry land, have been much less successful in the past year, thanks mainly to more effective protection of shipping convoys. A report published by the British House of Lords in late July revealed that eight pirated vessels and 215 hostages were currently held, compared with 23 vessels and 501 hostages at the same time a year ago. Only five ships were pirated in the first six months of this year. But the pirates have not gone away and are alleged to have spent big sums to influence the presidential race.

Somalia has often had false dawns. The much-reviled man ousted by the new president was once billed by Hillary Clinton, the American secretary of state, as the country’s “last best hope”. The new man is a political novice. But at least he seems honest, compared with what one new MP castigates as the previous “government of looters”. Like his predecessor, he hails from one of the four main clans, the Hawiye.

A moderate Islamist with links to the Muslim Brotherhood, Mr Hassan also won praise for staying in Mogadishu when most of its educated residents fled during the long period of bloody strife. He founded a private university that has flourished despite the conflict; more recently he set up the Peace and Development Party, which he says is Somalia’s first such outfit. Sally Healy, a British expert on Somalia who has co-authored a report with the new president, says he is “completely networked into the strongest and best bits of Somali society.” He needs to be.