The attack on the US consulate in Benghazi is a tragic reminder of Libya’s worsening security and the challenges of governance that the country faces. Although the country conducted successful, transparent parliamentary elections on July 7, it became less secure in the months that followed, particularly in the east. Almost unnoticed by western media, a series of car bombs, rocket attacks against police headquarters and assassinations of Gaddafi-era officials afflicted the city of Benghazi. These attacks, like the one on the consulate, are symptomatic of the dire weakness of the state and of the reign of local militias.

Bereft of a professional police and army, the country’s transitional authorities were forced to cut deals with the militias, the so-called revolutionary brigades that fought against Muammer Gaddafi. Many of these militias were genuinely committed to providing local defence and ensuring that the revolution succeeded. Several had begun the process of disarming. Others, particularly those affiliated with the country’s hardline Salafi population, are committed to pursuing more violent, radical goals or have become veritable mafias. One of these militias, the Imprisoned Omar Abdul Rahman Brigades, is implicated in the consulate attack.

In the eastern towns of Benghazi, Darnah and Baida, militias have attacked the Red Cross and desecrated the shrines and graves of Libya’s Sufis, followers of a variant of Islam that the Salafis regard as idolatrous. More recently, these attacks have spread to western cities. There have been signs that several attacks were sanctioned or tolerated by the provisional security forces, the Supreme Security Committee, formed hastily after the revolution as a way to harness the zeal of the rebels. By many accounts, this project has failed miserably, and in some cases, made the situation even worse. In some cases, the security forces have been infiltrated by the militias – when I visited Libya in July there were reports of SSC members “double dipping,” in the payroll of their local militia and the SSC itself.

In many ways, this security vacuum was the product of the weak legitimacy of the National Transitional Council. During the transitional period, many militias adopted a wait-and-see attitude, refusing to demobilise or disarm until a more permanent government in Tripoli was established – and one that ensured their local needs were met. Meanwhile, the national army and police remained underfunded, ill-equipped, and tainted by their association with the old regime.

In tackling these challenges, it is crucial not to overstate the threat from Salafism. Rather than signalling its ascendancy, the recent violence is actually a sign of the Salafis’ isolation from the mainstream. By temperament, culture and history, the Libyan people are averse to the sort of radical Islamism that motivated the attacks on the consulate and earlier violence against Sufis. Across the country, there have been demonstrations against the Salafi attacks and expressions of sympathy for the victims. On social media there are now messages of condolence for the slain US ambassador, who by all accounts was highly regarded by many Libyans with whom I spoke. “We are All the Martyr Hero Chris Stevens” reads one Arabic Facebook page.

Many Libyans are implicating the government in these attacks, demanding that it provide basic security, disarm the militias and build accountable police forces. With the election of a new parliament, the General National Congress and the installation of a new cabinet, all of this can change. The country’s new government has an opportunity to consolidate its legitimacy and win back the confidence of the Libyan people. Western diplomacy can and should play a more active role, in providing advice and assistance to guide the country through its constitutional process, rebalancing local and central governance, and reconstituting the security forces.

But such efforts must ultimately defer to the Libyans themselves, recognising their right to shape their own destiny. By nearly every account, Libyan and American, Chris Stevens embodied this approach. When I worked with him briefly at the US embassy in 2009, he was already famous for his infectious enthusiasm – even in those dark times, under the most despicable of regimes – for the enormous potential of the country’s people.

This article was originally published in the Financial Times.