text: The new leader received the title of "Agha" then "Dey" in 1671, and the right to select passed to the divan, a council of some sixty military senior officers.Thus Algiers became a sovereign military republic.It was at first dominated by the odjak; but by the 18th century, it had become the dey's instrument.Although Algiers remained nominally part of the Ottoman Empire, in reality they acted independently from the rest of the Empire, and often had wars with other Ottoman subjects and territories such as the Beylik of Tunis.The dey was in effect a constitutional autocrat.The dey was elected for a life term, but in the 159 years (1671–1830) that the system was in place, fourteen of the twenty-nine deys were assassinated.Despite usurpation, military coups and occasional mob rule, the day-to-day operation of the Deylikal government was remarkably orderly.Although the regency patronised the tribal chieftains, it never had the unanimous allegiance of the countryside, where heavy taxation frequently provoked unrest.Autonomous tribal states were tolerated, and the regency's authority was seldom applied in the Kabylia, although in 1730 the Regency was able to take control over the Kingdom of Kuku in western Kabylia.Many cities in the northern parts of the Algerian desert paid taxes to Algiers or one of its Beys.Barbary raids in the Mediterranean continued to attack Spanish merchant shipping, and as a result, the Spanish Empire launched an invasion in 1775, then the Spanish Navy bombarded Algiers in 1783 and 1784.For the attack in 1784, the Spanish fleet was to be joined by ships from such traditional enemies of Algiers as Naples, Portugal and the Knights of Malta.Over 20,000 cannonballs were fired, but all these military campaigns were doomed and Spain had to ask for peace in 1786 and paid 1 million pesos to the Dey.In 1792, Algiers took back Oran and Mers el Kébir, the two last Spanish strongholds in Algeria.