The city remained in the hands of Christian troops until 1102, when the Almoravids retook the city and restored the Muslim religion. Although the self-styled 'Emperor of All Spain', Alfonso VI of León and Castile, drove them from the city, he was not strong enough to hold it. The Christians set it afire before abandoning it, and the Almoravid Masdali took possession on 5 May 1109. The event was commemorated in a poem by Ibn Khafaja in which he thanked Yusuf ibn Tashfin for the city's liberation.The declining power of the Almoravids coincided with the rise of a new dynasty in North Africa, the Almohads, who seized control of the peninsula from the year 1145, although their entry into Valencia was deterred by Ibn Mardanis, King of Valencia and Murcia until 1171, at which time the city finally fell to the North Africans. The two Muslim dynasties would rule Valencia for more than a century.