Sultan Abu al-Hasan (ruled 1331–1348). Many of these madrasas were built near the major mosques which had already acted as older centers of learning, such as the Qarawiyyin, the Mosque of the Andalusians, and the Grand Mosque of Meknes. One of their most important functions seems to have been to provide housing for students from other towns and cities – many of them poor – who needed a place to stay while studying at these major centers of learning. In Fes, the first madrasa was the Saffarin Madrasa built in 1271, followed by the Sahrij Madrasa founded in 1321 (and the Sba'iyyin Madrasa next to it two years later), the al-Attarine in 1323, and the Mesbahiya Madrasa in 1346. Another madrasa, built in 1320 near the Grand Mosque of Fes el-Jdid, was less successful in contributing to the city's scholarly life. These madrasas taught their own courses and sometimes became well-known institutions in their own right, but they usually had much narrower curriculums or specializations than the Qarawiyyin. The last and largest Marinid madrasa in Fes, the Bou Inania, was a slightly more distinctive institution and was the only madrasa to also have the status of a Friday mosque. Surviving Marinid madrasas built in other cities include the Madrasa of Abu al-Hasan in Salé and the Bou Inana Madrasa of Meknes. Many more were built in other cities but have not been preserved, or only partially preserved, including in: Taza, al-Jadida, Tangier, Ceuta, Anfa, Azemmour, Safi, Aghmat, Ksar el-Kebir, Sijilmasa, Tlemcen, Marrakesh (the Ben Youssef Madrasa which was rebuilt in the 16th century), and Chellah (near Rabat). Literary production under the Marinids was relatively prolific and diverse. In addition to religious texts such as treaties of fiqh (jurisprudence), there was also poetry and scientific texts. Geographies and, most of all, histories were produced, partly because the dynasty itself was eager to use these to legitimize its rule. Ibn Khaldun was the most famous manifestation of this intellectual life which was also shared with the Emirate of Granada in Al-Andalus, where many of the intellectuals of this period also spent time. Ibn al-Khatib, the Andalusi poet and writer from Granada, also spent time in Fes and North Africa when his Nasrid master Muhammad V was there in exile between 1358 and 1362. The historian Ibn Idhari was another example, while the famous traveler Ibn Battuta also passed through Morocco in the