He also moved the capital from Fez to Meknes, where he built a vast imperial kasbah, a fortified palace-city whose construction continued throughout his reign. He also built fortifications across the country, especially along its eastern frontier, which many of his Abid troops garrisoned. This was partly a response to continued Ottoman interference in Morocco, which Isma'il managed to stop after many difficulties and rebellions. Al-Khadr Ghaylan, a former leader in northern Morocco who fled to Ottoman Algiers during Al-Rashid's advance, returned to Tetouan at the beginning of Isma'il's reign with Ottoman help and led a rebellion in the north which was joined by the people of Fes. He recognized Isma'il's nephew, Ahmad ibn Mahriz, as sultan, who in turn had managed to take control of Marrakesh and was recognized also by the tribes of the Sous valley. Ghaylan was defeated and killed in 1673, and a month later Fez was brought back under control. Ahmad ibn Mahriz was only defeated and killed in 1686 near Taroudant. Meanwhile, the Ottomans supported further dissidents via Ahmad al-Dala'i, the grandson of Muhammad al-Hajj who had led the Dala'iyya to dominion over a large part of Morocco earlier that century, prior to Al-Rashid's rise. The Dala'is had been expelled to Tlemcen but and they returned to the Middle Atlas at the instigation of the Ottomans and under Ahmad's leadership in 1677. They managed to defeat Isma'il's forces and control Tadla for a time, but were defeated in April 1678 near Wadi al-'Abid. Ahmad al-Dala'i escaped and eventually died in early 1680. After the defeat of the Dala'is and of his nephew, Isma'il was finally able to impose his rule without serious challenge over all of Morocco and was able to push back against Ottoman influence. After Ghaylan's defeat he sent raids and military expeditions into Ottoman Algeria in 1679, 1682, and 1695–96. A final expedition in 1701 ended poorly. Afterwards, peace was re-established and the Ottomans agreed to recognize Morocco's eastern frontier near Oujda.