TRIPOLI, Libya — Half-a-dozen coastguard speedboats careened around Tripoli harbor, spinning, heeling over, kicking up surf and spray and looking as if, at any moment, their enthusiastic pilots might race themselves into oblivion. A handful of superannuated navy ships let rip with their ear-splitting horns, cheering the speedboats on, while military officers gathered on the pier to snap cellphone shots of the flashing hulls and rooster-tail wakes.

The moment was like a metaphor for Libya since the ouster four years ago of Colonel Muammar Gaddafi: a country spinning recklessly in circles, often at the risk of complete calamity, and somehow surviving. But how long this show can go on remains an open question.

The country has two governments at the moment, one in Tripoli, the other in Tobruk, far to the east. And there are numerous militias, many with an Islamist tinge, some tied with al Qaeda or the bloodthirsty followers of the so-called Islamic State based in Syria and Iraq. The chaos is not recent, nor should it be underestimated. That may have been the most fatal of several mistakes that led to the death of the U.S. ambassador in Benghazi in 2012.

Last week, the United Nations envoy to Libya, Bernardino León, warned at another round of peace talks endeavoring to reach a deal between Libya’s two rival regimes that the country is “really at the limit” and is on the verge of becoming a failed state. Bankruptcy beckons and within six weeks the rival governments won’t have enough cash to pay the salaries of public workers—including the wages of their fighters.

That won’t mean the militiamen, who form the bulk of both sides’ forces, will lay down their weapons. More likely it will worsen a conflict consisting not only of a standoff between the competing parliaments but also of a dizzying number of ever-changing micro-conflicts featuring tribes and towns and militias large and small, all with different and frequently shifting allegiances and grievances.

And while the Libyans squabble and clash, the disputes are being exploited by the Islamic State’s affiliate in Libya, which now controls Gaddafi’s coastal hometown of Sirte, and seized another small town nearby last week. Earlier this year the extremists beheaded 21 Egyptian Christians there, warning as they did so that Rome is in their sights. They slaughtered at least 28 Ethiopians in February and captured another 86 Eritrean Christian migrants on Wednesday.

Also on Wednesday ISIS beheaded a onetime postal worker outside a mosque in the eastern town of Derna, taking local kids to watch the gory slaughter. And according to Libyan security sources, ISIS now has about 2,000 fighters in Sirte and an estimated 700 in Sabratha, famous for its Roman ruins, in the northwestern corner of Libya just 41 miles from Tripoli.

“Many of them are Syrian veterans,” a security officer called Mustafa told The Daily Beast. He asked for his family name not to be disclosed. “Of the foreigners, 70 percent are Tunisians but there are fighters from Sudan and Iraqis and Syrians.”

Mustafa was in Tripoli ’ s luxurious Corinthia Hotel on January 27 when it came under what he describes as an “expertly planned and executed attack” by Islamic extremists that left 10 people dead. After spraying the entrance to the hotel two assailants entered while a car bomb outside was detonated remotely by an accomplice. The attackers went up to a higher floor, killing as they went, although they showed mercy to a newly married Libyan couple.

One of the attackers, a Sudanese, detonated a suicide vest prematurely, hurling the other, a Tunisian, against a wall before he could detonate his. “When we found the Tunisian he was dying and had a grenade in his hand that thank God failed to explode. He couldn’t speak. We watched him die,” Mustafa says. He believes a Western delegation who had left the hotel was the target of the attack—one member of their security detail, American contractor Dave Berry, was killed.

No foreign airlines are braving the skies above Libya. A small Maltese charter company is flying daily a turboprop executive plane to the Mitiga air force base at Tripoli—the 55-minute flight costs $620. And Libyan carriers have maintained a handful of flights from eastern airports to Istanbul and Alexandria. The costal road to Tunisia is fraught with peril—last month the Ukrainian ambassador was robbed at gunpoint and on Wednesday alone there were half-a-dozen kidnappings on the western and southern approaches to the Libyan capital.

Near Tripoli, fighting risks flaring between militias from the hard-scrabble mountain town of Zintan, which have aligned with the government in Tobruk and its controversial military commander, General Khalifa Haftar, and brigades loyal to the Tripoli government.

The Zintanis, who fielded key militias in the downfall of the Gaddafi regime, can also call on the support of the so-called Tribal Army, a coalition dominated by the Wershefana tribe, which was loyal to the former Libyan strongman and fought against Zintan four years ago. Now the Tribal Army is fighting alongside Zintan’s militiamen trying to capture Al-Watiya airbase southwest of the capital, which is controlled by the Libya Dawn forces of the Tripoli government.

The few Western reporters here echo the warnings of U.N. envoy León and file stories justifiably reporting the country is “disintegrating.” But that word doesn’t quite capture the peculiarly Libyan ambience and the extent to which the conflict across the country is a very, very Libyan one: earnest and deadly, certainly, but also comical and orderly—yes, orderly. Where else would you find one rival government responding to phone call pleas from the other for its share of the country’s subsidized goods, or one government continuing to transfer salaries to the bank accounts of the rival government’s fighters?

“We provide them with everything,” says Jamal Zubia, a spokesman for the Tripoli-run government’s foreign ministry. A former exile who raised his seven children in Britain, Zubia rubs his hand over his bearded face and chuckles when asked whether he considers it odd for a rival government to fund the activities of the other.

“Yes, we pay the salaries of their fighters,” he says. “We pay the social security payments and child benefits for everyone in Libya. We pay for the schools, we pay the fighters and even the police service, too, in the east. We transfer the money for the fighters because many of them were members of the Libyan armed forces or were warriors in the uprising against Gaddafi.”

The Tobruk government recently published its own budget but it is basically a fiction. Money is disbursed by the central bank and the bank only responds to orders from Tripoli’s Minister of Finance. Like the country’s investment authority, the central bank is now based out of Malta. On Friday Tobruk announced a new central bank under its control but how it will be funded is unclear.

Abdulgader Hwili, a senior member of the mainly Islamist government of the General National Congress (GNC), the parliament in Tripoli, also reacts with a laugh about the cash transfers to fighters loyal to General Haftar. This is a family squabble, he tells me. “I have eight brothers—four of them support Tobruk,” he says. “So they are our family—we have to pay them.”

Hwili says the familial nature of the confrontation between Tripoli and Tobruk is limiting the violence and he hopes it will lead to a deal between the two governments—the one now in Tobruk was a House of Representatives elected last year to replace the Islamist-dominated parliament that is still in Tripoli but has refused to step down.

The stay-put parliament’s partisans, known as Libya Dawn, or Fajr, often are associated with the effort by Turkey and Qatar to spread the influence of the Muslim Brotherhood. Their opponents sometimes fall in line with efforts by the Saudis, Emiratis, and Egyptians to crush it. Haftar takes Egypt’s new president, General Abdel Sisi, as his anti-Islamist role model. But the situation is at once more complicated than that, and, from a Libyan perspective, simpler.

Hwili says that while the U.N.-sponsored talks are likely to fail—the next round of negotiations is slated to start this week in Morocco—he has hopes that face-to-face talks between the two sides already being planned for later will succeed in hammering out a peace deal and perhaps a national unity administration.

Like other lawmakers here, Hwili doesn’t think either side can win the war—a point emphasized in a soon-to-be-published study by Libyan academics sponsored by the German think tank Friedrich Ebert Stiftung. “No military action is able to make any territorial gains and end the war in its favor; this paves the way for a political dialogue to end the crisis,” the report argues.

Meanwhile, for all the talk of state disintegration Tripoli is enjoying more order than the capital has seen for much of the four years since Gaddafi’s ouster. Trash is collected—something that Libya’s first post-civil war government, the National Transitional Council, failed to achieve for almost a year. Even the police are visible: In the past four years they have been the first to disappear when violence is flaring or in the offing. The streets are free of the threatening presence of unruly camouflaged gunmen from this or that militia mounting ad hoc checkpoints or muscling their way around downtown in battered pickup trucks. And turf-fighting clashes between militias at night are rarely heard at the moment. But, as ever with Libya, much is deceptive and calm is fragile.

The order in the capital could break down easily and residents fear a replay of last summer’s ferocious fighting in Tripoli, which was more intense than the capital saw during the uprising to oust Gaddafi. Even though the capital now is free of bullying militias, people are not free to speak their minds unless they are speaking in favor of Libya Dawn. In districts like Fashloon, which backs the Tobruk government, the atmosphere is tense and surly.

Libyan journalist Essam Zuber, who appears regularly on local television, says he has been receiving threats since he started criticizing Libya Dawn.While pro-Libya-Dawn demonstrations are mounted regularly in Tripoli’s Martyrs’ Square, no one has dared to mount pro-Tobruk ones. And last week the body of a parliament member who broke with his colleagues was found on a road an hour or so away from Tripoli—he bore signs of torture.

But expressing support in the east for Libya Dawn is dangerous, too. There are many families who have moved to Tripoli from Benghazi after receiving threats there from supporters of General Haftar. Both legislatures have legitimacy problems and neither is likely to have met its quorum requirements for months. Neither parliament permits the broadcasting of its sessions, presumably to hide the fact of low attendance by its members. Western journalists are allowed to move freely without minders in Tripoli but in Tobruk, foreign reporters are chaperoned by security minders. In Beida, where the House of Representative government sits, free unaccompanied movement by foreign reporters is allowed.

In Tripoli security forces are hard-pressed to contain drug dealers and criminal abduction rings.

“We have more than 900 criminal suspects detained here at the jail— most arrested in the last few weeks,” says a Tripoli security official at the Mitiga air force base. Ever larger ransoms are being demanded. In the gloom of dusk he pointed to a man climbing slowly into his car, saying: “He is a businessman and his 16-year-old son has been held for weeks now—the kidnappers are demanding €4 million [$4.42 million]. We caught those lower down in the gang but the boss is still free and the boy remains missing.”

Above all there is fear of what ISIS might do next, and two recent suicide bombings in the town of Misrata, the hardcore pro-Tripoli town three hours’ drive away, has everyone here nervous. The most recent suicide bombing—also carried out by a Tunisian—killed at least half a dozen at the Dafniya checkpoint on the main road from Tripoli. That attack appears to have given real impetus to the search for a deal to establish a national unity government.

At any rate that is the view of Abd al-Wahhab Muhammad Qaid, a former member of the anti-Gaddafi Libyan Islamic Fighting Group, whose brother, Abu Yahya al-Libi, became a luminary of al Qaeda and was killed by an American drone strike in Pakistan in June 2012.

Some analysts believe the Libyan jihadi attack on the U.S. consular residence in Benghazi in September 2012—an assault that caused the death of Ambassador Christopher Stevens—was in revenge for the killing of al-Libi.

Qaid, now a senior member of the Tripoli parliament, joined the Libyan Islamic Fighting Group after years as a student activist in Benghazi. His brother, an Islamic scholar, lagged behind him by two years. Both ended up in Afghanistan. Qaid was captured in 1995 when he returned to Libya on a clandestine mission, and he spent the next decade-and-a-half in Tripoli’s notorious Abu Salim prison. His brother remained in Afghanistan but was swept up after the fall of the Taliban and handed over to the Americans by the Pakistani authorities. He was held by the Americans at the notorious prison in Bagram Air Base in Afghanistan, and supposedly joined al Qaeda after that.

Qaid later tempered his beliefs and, like others who once served the Libyan Islamic Fighting Group, now considers himself an Islamist modernizer. He and other former leaders are adamant that they never agreed with the Western-focused terrorism of Osama bin Laden, and were exclusively concerned with ousting Gaddafi. They say they refused several entreaties by bin Laden to join him.

Qaid’s background, with his understanding of the jihadist mentality, means his words now carry special weight. As we sat in an anonymous office building near downtown Tripoli—he moves around frequently, fearing ISIS may target him—he says the clear intention of the so-called Islamic State in Libya has been announced with a slogan: “We are here to stay and here to spread.” He emphasizes his words with his large, dark hands.

The ISIS threat, he says, is driving a Libyan consensus that a peace deal has to be reached between Libya’s dueling governments. “I have faith negotiations will succeed,” says Qaid. The emergence of Daesh, he says, using the Arab acronym for ISIS, makes it crucial that an agreement is reached. “The seed of Daesh has been planted but it is still young, but if we ignore this threat it will grow quickly and will be more difficult to uproot. It is important we confront it now.”

Qaid believes the Libyan affiliate takes its orders from Abu Bakr al-Baghdadi and his lieutenants in Syria. “We think its near-term strategic aim is to provoke the West to focus its attention on Libya and divert the international coalition so the attacks on Daesh in Syria and Iraq will diminish.”

Tactically, ISIS will aim to take over another town. He expects more suicide bombings in Tripoli and Misrata and more atrocities like the beheading of Christians in January on the shores of Sirte, to push the West to intervene.

Time is running out for the Libyans to stop spinning in circles.