Somalia is the kind of place that makes outsiders cynical. The capital, Mogadishu, is a rubble-strewn wreck of a city, which occasionally reveals glimpses of the grand coastal trading town it must once have been.
But to see those glimpses, you've got to look past the tens of thousands of displaced people camped on the tiniest plots of vacant land past the crumbling bullet-scarred walls and buildings that line the streets.
You've got to see beyond the ubiquitous gunmen who seem to be living proof of Mogadishu's murderous reputation and the long list of warlords and robber barons who've populated what so often only vaguely passes as a government.
But then, along comes an election that confounds the lazy clichés, and raises the possibility that Somalia might – just might – have turned a corner.
Deeply flawed test
By any measure, it was a deeply flawed test of public opinion here. A general election was impossible – Somalia is still too fractured and far too dangerous for that – so parliament instead voted to choose the national leader.
And those MPs were themselves appointed by a council of clan elders. The whole process was dogged by allegations of vote-buying, of intimidation, of cynical attempts to mangle every stage and come up with a result that favoured only the victors' bank accounts.
For much of the campaign, it appeared that the battle would be between the two most powerful political figures in the country – outgoing President Sharif Sheikh Ahmed and the prime minister, Abdiweli Ali Mohammed. Both had hefty international backers with deep pockets both had the power and authority of incumbency.
The dark horse turned out to be a political novice a mild-mannered academic with a quick wit and an engaging laugh, who was virtually unknown until a few weeks ago when he formally entered the race.
We first met Hassan Sheikh Mohamud about a year ago, when he had just formed the Peace and Development Party. Back then, he seemed to be an unlikely national leader – an idealistic opposition figure too concerned with social philosophy to make it through Somalia's political coffee grinder.
Reliance on firepower
Ever since the Marxist leader Siad Barre seized power in a coup in 1969, the country has been led by military "strongmen" who relied more on their firepower than on persuasion.
Sharif Sheikh Ahmed was the first to crack that mould. He started his career as a cleric, and as president he appointed a government that included many able technocrats from the diaspora. But he still came to power as chairman of the Islamic Courts Union – the militant Taliban-style force that held Mogadishu for six months before Ethiopian troops forced them out.
This week, rumours of rampant vote-buying swirled around the Police Academy where MPs convened for the election. There was talk of death threats for those who voted against Candidate X or Candidate Y, and promises of ministries or contracts or bags of cash to those who voted in favour.
And yet, the day before the historic vote, Hassan Sheikh emerged as a credible compromise candidate.
He was still an outside chance, to be sure. But all of a sudden parliamentarians were speaking his name in serious tones. Few people apart from Hassan Sheikh's own campaign team seriously believed he would win, but he was also the most credible outsider in the race.
After two rounds of polling, he won with a landslide: 190 votes, against his only remaining rival, Sheikh Sharif Ahmed, who won 79 votes. It was such an unequivocal vote against the outgoing president, that in his speech admitting defeat, the humbled cleric told MPs: "It seems you don’t trust me much."
But, more importantly, the scale of Hassan Sheikh's victory trumped any allegations of manipulation, and gave him an air of credibility and legitimacy not seen here for decades.
So at the end of it all, the manner of this election mattered more than its outcome. For the first time in more than 40 years, there has been a peaceful political transition, to a government formed inside Somalia, through a relatively transparent process, largely free of outside coercion.
It was an undeniably flawed process, and hardly a model of democracy, but if its purpose was to bring forth a leader who could legitimately claim a measure of popularsupport, it seemed to work.
None of this is to minimise the scale of the task ahead. Somalia remains the "most failed" state in the world Al Shabaab still controls large parts of the country including a key port and more than two million people need urgent food aid to survive.
But for the first time in more than two decades, Somalia has a functioning government and an elected president. And that in itself is no small victory.
Follow Peter Greste on Twitter: @PeterGreste