In the early 19th century war broke out between the United States and Tripolitania, and a series of battles ensued in what came to be known as the First Barbary War and the Second Barbary War. By 1819, the various treaties of the Napoleonic Wars had forced the Barbary states to give up piracy almost entirely, and Tripolitania's economy began to crumble. As Yusuf weakened, factions sprung up around his three sons; though Yusuf abdicated in 1832 in favor of his son Ali II, civil war soon resulted. Ottoman Sultan Mahmud II sent in troops ostensibly to restore order, but instead deposed and exiled Ali II, marking the end of both the Karamanli dynasty and an independent Tripolitania. Anyway, order was not recovered easily, and the revolt of the Libyan under Abd-El-Gelil and Gûma ben Khalifa lasted until the death of the latter in 1858.