After the fall of Zaragoza in 1118 by the hands of King Alfonso The Battler, Avempace looked for shelter under Abu Ishaq Ibrahim ibn Yûsuf Ibn Tashfin, another brother of the Almoravid Sultan (Ali Ibn Yusuf Ibn Tashfin) in Xàtiba. He worked, for some twenty years, as the vizier of Yusuf Ibn Tashfin. Throughout these decades, it is clear that Avempace was not as agreeable with those close to the ruler, Ibn Tashfin, as he was during the previous reign of Ibn Tifilwit. Writings by Ahmad al-Maqqari gives us insight into the hostility and disagreements between Avempace and the father of a famous physician respected by Ibn Tashfin, Abd al-Malik. A poetry anthology, Qala’id al-iqya (Necklace of Rubies), was also created by a courtier of Ibn Tashufin's, Abu Nasr al-Fath Ibn Muhammad Ibn Khaqan, which condescendingly placed Avempace in last place. Under Ibn Tashfin, the Sultan of the Almoravid empire, Avempace was imprisoned twice. The details of the imprisonment are not well understood, but can be assumed. Despite being unwelcomed, Avempace remained with the Almoravid empire for the rest of his life until his death in 1138. There has been evidence presented that brings up the argument that the cause of Avempace's death was by poison from his peers. Al-Maqqari details in his writing that a physician, Abu l-'Ala' Ibn Zuhr, was an enemy of Avempace's whose servant, Ibn Ma‛yub, was suspected of poisoning him at the time but was never convicted. Among his many teachers was Abu Jafar ibn Harun of Trujillo, a physician in Seville, Al-Andalus.