among themselves. The last Hammudid caliph reigned until 1027. The Hammudids were then expelled from Cordoba, where there was still a great deal of anti-Berber sentiment. The Hammudids remained in Málaga until expelled by the Zirids in 1056. The Zirids of Granada controlled Málaga until 1073, after which separate Zirid kings retained control over the taifas of Granada and Malaga until the Almoravid conquest. During the taifa period, the Aftasid dynasty, based in Badajoz, controlled a large territory centered on the Guadiana River valley. The area of Aftasid control was very large, stretching from the Sierra Morena and the taifas of Mértola and Silves in the south, to the Campo de Calatrava in the west, the Montes de Toledo in the northwest, and nearly as far as Oporto in the northeast. According to Bernard Reilly, during the taifa period genealogy continued to be an obsession of the upper classes in al-Andalus. Most wanted to trace their lineage back to the Syrian and Yemeni Arabs who accompanied the invasion. In contrast, tracing descent from the Berbers who came with the same invasion "was to be stigmatized as of inferior birth". Reilly notes, however, that in practice the two groups had by the 11th century become almost indistinguishable: "both groups gradually ceased to be distinguishable parts of the Muslim population, except when one of them actually ruled a taifa, in which case his low origins were well publicized by his rivals". Nevertheless, distinctions between Arab, Berber, and slave were not the stuff of serious politics, either within or between the taifas. It was the individual family that was the unit of political activity." The Berber that arrived towards the end of the caliphate as mercenary forces, says Reilly, amounted to only about 20 thousand people in a total al-Andalusi population of six million. Their high visibility was due to their foundation of taifa dynasties rather than large numbers. In the power hierarchy, Berbers were situated between the Arabic aristocracy and the Muladi populace. Ethnic rivalry was one of the most important factors driving Andalusi politics. Berbers made up as much as 20% of the population of the occupied territory. In al-Andalus under the Almoravids During the taifa period, the Almoravid empire developed in northwest Africa, whose core was formed by the Lamtuna branch of the Sanhaja Berber. In the mid-11th century, they allied with the Guddala and Massufa Berber. At that time,