<P> After the recapture in 1261 by the Byzantines, the church was in a dilapidated state . In 1317, emperor Andronicus II ordered four new buttresses (Pyramídas, Greek: "Πυραμίδας") to be built in the eastern and northern parts of the church, financing them with the inheritance of his deceased wife, Irene . New cracks developed in the dome after the earthquake of October 1344, and several parts of the building collapsed on 19 May 1346; consequently, the church was closed until 1354, when repairs were undertaken by architects Astras and Peralta . </P> <P> Constantinople fell to the attacking Ottoman forces on the 29th of May in 1453 . In accordance with the traditional custom at the time, Sultan Mehmet II allowed his troops and his entourage three full days of unbridled pillage and looting in the city shortly after it was captured . Once the three days passed, he would then claim its remaining contents for himself . Hagia Sophia was not exempted from the pillage and looting and specifically became its focal point as the invaders believed it to contain the greatest treasures and valuables of the city . Shortly after Constantinople's defenses collapsed and the Ottoman troops entered the city victoriously, the pillagers and looters made their way to the Hagia Sophia and battered down its doors before storming in . All throughout the period of the siege of Constantinople, the trapped worshippers of the city participated in the Divine Liturgy and the Prayer of the Hours at the Hagia Sophia and the church formed a safe - haven and a refuge for many of those who were unable to contribute to the city's defense, which comprised women, children, the elderly and the sick and the wounded . Being hopelessly trapped in the church, the many congregants and yet more refugees inside became spoils - of - war to be divided amongst the triumphant invaders . The building was significantly desecrated and looted to a large extent, with the helpless occupants who sought shelter within the church being either enslaved, physically and sexually violated or simply slaughtered . While most of the elderly and the infirm / wounded and sick were killed, a vast number of women and girls were raped and the remainder (mainly teenage males and young boys) were chained up and sold off into slavery . The church's priests and religious personnel continued to perform Christian rites, prayers and ceremonies until finally being forced to stop by the invaders . When Sultan Mehmet II and his accompanying entourage entered the church, he insisted that it should be converted into a mosque at once . One of the ulama present then climbed up the church's pulpit and recited out the Shahada, thus marking the beginning of the gradual conversion of the church into a mosque . </P> <P> As described by several Western visitors (such as the Córdoban nobleman Pero Tafur and the Florentine Cristoforo Buondelmonti), the church was in a dilapidated state, with several of its doors fallen from their hinges; Mehmed II ordered a renovation as well as the conversion . Mehmet attended the first Friday prayer in the mosque on 1 June 1453 . Aya Sofya became the first imperial mosque of Istanbul . To the corresponding Waqf were endowed most of the existing houses in the city and the area of the future Topkapı Palace . From 1478, 2,360 shops, 1,300 houses, 4 caravanserais, 30 boza shops, and 23 shops of sheep heads and trotters gave their income to the foundation . Through the imperial charters of 1520 (AH 926) and 1547 (AH 954) shops and parts of the Grand Bazaar and other markets were added to the foundation . </P> <P> Before 1481 a small minaret was erected on the southwest corner of the building, above the stair tower . Later, the subsequent sultan, Bayezid II (1481--1512), built another minaret at the northeast corner . One of these collapsed after the earthquake of 1509, and around the middle of the 16th century they were both replaced by two diagonally opposite minarets built at the east and west corners of the edifice . </P>

Who turned the hagia sophia into a mosque