became the last and longest reigning Muslim dynasty in the Iberian peninsula, the Nasrids, who ruled the Emirate of Granada. On multiple occasions Ibn al-Ahmar aligned himself with Ferdinand III, eventually agreeing to become his vassal in 1246. Granada thereafter became a tributary state to the Kingdom of Castile, although this was often interrupted by wars between the two states. The political history of the emirate was turbulent and intertwined with that of its neighbours. The Nasrids sometimes provided refuge or military aid to Castilian kings and noblemen, even against other Muslim states, while in turn the Castilians provided refuge and aid to some Nasrid emirs against other Nasrid rivals. On other occasions the Nasrids attempted to leverage the aid of the North African Marinids to ward off Castile, although Marinid interventions in the Peninsula ended after Battle of Rio Salado (1340). The population of the emirate was also swelled by Muslim refugees from the territories newly conquered by Castile and Aragon, resulting in a small yet densely-populated territory which was more uniformly Muslim and Arabic-speaking than before. The city itself expanded and new neighbourhoods grew around the Albaicín (named after refugees from Baeza) and in Antequeruela (named after refugees from Antequera after 1410). A new set of walls was constructed further north during the 13th–14th centuries, with Bab Ilbirah (present-day Puerta de Elvira) as its western entrance. A major Muslim cemetery existed outside this gate. The city's heart was its Great Mosque (on the site of the present-day Granada Cathedral) and the commercial district known as the qaysariyya (the Alcaicería). Next to this was the only major madrasa built in al-Andalus, the Madrasa al-Yusufiyya (known today as the Palacio de la Madraza), founded in 1349. Other monuments from this era include the al-Funduq al-Jadida ("New Inn" or caravanserai, now known as the Corral del Carbón), built in the early 14th century, the Maristan (hospital), built in 1365–1367 and demolished in 1843, and the main mosque of the Albaicín, dating from the 13th century. When Ibn Al-Ahmar established himself in the city he moved the royal palace from the old Zirid citadel on the Albaicín hill to the Sabika hill, beginning construction on what became the present Alhambra. The Alhambra acted as a self-contained palace-city, with its own mosque, hammams, fortress, and residential quarters for workers and servants. The most celebrated palaces that survive today, such as the Comares Palace