 In any case, one of the last Almoravid emirs, Tashfin ibn Ali (ruled 1143-45) built a new ribat on the site of the current kasbah during his efforts to repel the Almohads, but the Almohads defeated the Almoravids and destroyed the ribat.In 1150 or 1151 the Almohad caliph Abd al-Mu'min built a new kasbah (citadel) over the site of the former ribat, within which he included a palace and a mosque.He also had an underground canal dug to divert a water source to the area, allowing for future settlement and urbanization.