and recaptured his throne in 1361 A.C.E. (763 A.H.). He then recalled his Wazir Ibn al-Khatib. 
When the relationship between Sultan Muhammad and Ibn Khaldun turned sour and uncertain, he turned toward Andalusia. He was welcomed and honored by Sultan Muhammad, who admitted him to his private council. In the following year, Sultan Muhammad sent Ibn Khaldun on an ambassadorial mission to Pedro, the King of Castile. Ibn Khaldun concluded the mission and peaceful terms were established between them. Pedro offered Ibn Khaldun a position in his service and the return of his family’s former estate at Castile. Ibn Khaldun declined the offer. 
Upon his return from Castile, Ibn Khaldun offered Pedro’s gift to him to the sultan and in return, the sultan gave him the village of Elvira. Soon Ibn Khaldun was restless once more and in the following year, 1364 A.C.E. (766 A.H.), when he received an invitation from his friend Abu Abdullah, who had recaptured his throne at Bougie, Ibn Khaldun left Granada after asking permission to leave from Sultan Muhammad. 
Ibn Khaldun arrived in Bougie at the age of thirty-two. His plans had finally been realized. The period of imprisonment in Fez did not go to waste. He entered the city as a favorite guest. He accepted the position of hajib for Emir Muhammad. However, his life of power did not last long, as in the following year Abul Abbas killed the Emir Muhammad, his cousin. Ibn Khaldun handed the city to him and retired to the city of Biskra. He continued his political work in relaying the tribes to the service of this or that emir or sultan. He continued his practice of shifting loyalties as times and opportunities afforded him. He finally retired to a far outpost south of Constantine, Fort Salama. 
In Fort Salama, at the age of forty-five, he enjoyed a peaceful existence, and began to write his famous Muqqddimah, and the first version of his Universal History.
He dedicated his work to the current Emir of Constantine, Sultan Abul Abbas. But tranquility did not last long with Ibn Khaldun, as he needed reference works that were not available at his far outpost. He used the occasion of Abul Abbas’s conquest of Tunisia to go to Tunis. This was the first time he had returned to the town of his birth since leaving it more than twenty-seven years previously.
There were political forces at work against him once more and this time, before he fell out of favor, he used a convenient occasion (in 1382 A.C.E / A.H.) to leave North Africa behind, never to return. 
Ibn Khaldun was granted permission from Sultan Abul Abbas to go on hajj. He arrived in Alexandria in October 1382 A.C.E. (Shabaan 784 A.H.), at the ripe age of fifty. He spent a month preparing to leave for hajj, but was unable to join the caravan bound for the Holy Lands. He turned toward Cairo instead. Here he was warmly welcomed by scholars and students, and it was in Cairo that he lived out his final days. His fame for his writings had already preceded him. He lectured at al-Azhar and other fine schools. He had the chance to meet with Sultan al-Zahir Barquq who appointed him to a teaching post at the Kamhiah school. 
He again enjoyed the favors of the sultan. He was appointed a Maliki Judge at the sultan’s whim, and anger. He fared well and tried to fight corruption and favoritism, but again, conspiracies worked against him and he was relieved of duty, just in time to coincide with his family’s disaster. The ship carrying his family and belongings sank in a storm. 
Ibn Khaldun again took permission to go on hajj to the Holy Lands. He returned and was well received, and appointed to a teaching position in the newly built school, Bein al-Qasrein. He lectured in hadith, particularly Imam Malik’s Muwatta. He was then appointed to the Sufi Institute of Beibers with a generous salary. But soon, the state of affairs of Egypt was disturbed, as a rival of Sultan Barquq, Yulbugha, organized a successful revolt. Sultan Barquq staged a counter-revolt and was restored to his former throne. During this period, Ibn Khaldun suffered and then had his position res