years before releasing him as a vassal of the Seljuks. The next year, he marched against rebellious Oghuz Turks from Khuttal and Tukharistan. But he was defeated twice and was captured after a second battle in Merv. The Oghuzs looted Khorasan after their victory. Balkh was nominally ruled by Mahmud Khan, the former khan of Western Karakhanids, but the real power was held by Muayyid al-Din Ay Aba, amir of Nishabur for three years. Sanjar finally escaped from captivity and returned to Merv through Termez. He died in 1157 and control of Balkh passed to Mahmud Khan until his death in 1162. It was captured by Khwarezmshahs in 1162, by the Kara Khitans in 1165, by the Ghurids in 1198 and again by Khwarezmshahs in 1206. Muhammad al-Idrisi, in the 12th century, speaks of its possessing a variety of educational establishments, and carrying on an active trade. There were several important commercial routes from the city, stretching as far east as India and China. The late 12th-century local chronicle The Merits of Balkh (Fada'il-i-Balkh), by Abu Bakr Abdullah al-Wa'iz al-Balkhi, states that a woman known only as the khatun (lady) of Davud, from 848 appointed governor of Balkh, had taken over from him with "particular responsibility for the city and people" while he was busy building himself an elaborate pleasure palace called Nawshǎd (New Joy). Mongol invasion In 1220 Genghis Khan sacked Balkh, butchered its inhabitants and levelled all the buildings capable of defence – treatment to which it was again subjected in the 14th century by Timur. Not withstanding this, however, Marco Polo (probably referring to its past) could still describe it as "a noble city and a great seat of learning." For when Ibn Battuta visited Balkh around 1333 during the rule of the Kartids, who were Tadjik vassals of the Persia-based Mongol Ilkhanate until 1335, he described it as a city still in ruins: "It is completely dilapidated and uninhabited, but anyone seeing it would think it to be inhabited because of the solidity of its construction (for it was a vast and important city), and its mosques and colleges preserve their outward appearance even now, with the inscriptions on their buildings incised with lapis-blue paints." It was not reconstructed until 1338. It was captured by Tamerlane in 1389 and its citadel was destroyed, but Shah Rukh, his successor, rebuilt the citadel in 1407. 16th to 19th