This gave the townspeople the right to elect their own parish priest, have their own jurisdiction, inherit possessions, and pay no customs in all of Hungary. Later kings recognized these rights, but the townspeople still had to struggle to maintain them. After Louis II's marriage to Mary of Habsburg, Óvár became a key defense on the Austrian border, which would come into play during the Turkish invasion. In 1529, after the Turks were repulsed at the Siege of Vienna, they destroyed Óvár almost completely, leveling all of its Medieval buildings, including the castle and the Romanesque church. The armies of János Szapolyai and the Archduke Ferdinand also sacked the town. However, once again the inhabitants went about rebuilding it. During the Reformation, the town was almost completely converted to Lutheranism, and the famous preacher Huszár Gal opened a Lutheran school at Magyaróvár in 1555. Counterreformative movements forbade Protestantism in 1672, closing down the school and the Lutheran church. Due to the lax nature of the new statutes, and the rights of the townspeople as enforced by Ferdinand and Archduke Maximilian, however, religion did not become compulsory. During this time, Moson and Óvár alike were attacked by various armies, including Turkish and German mercenaries. After the fall of Győr in 1594, the castle was modernized to withstand a possible future attack by Italian engineers. During the 17th century Magyaróvár enjoyed great urban development and some independence. In 1683, the new castle was helpless against the retreating Turkish army, which had been repulsed again at Vienna. Both Moson and Magyaróvár were set ablaze. Though the town archives were now completely destroyed, the damage was repaired more quickly this time around, at least quickly enough to allow Rákóczi to use the castle as a base during his war for independence from the Habsburgs. In 1721, after the revolution was crushed, the castle at Magyaróvár lost its strategic importance, and all military materiel was transferred to Bratislava. However, the town prospered greatly after the war, with the establishment of new guilds, a town doctor, and the Piarist school. The Austrian government wished to limit the independence of the town, but the people were able to hold on to a degree of autonomy; an agreement to this effect was signed in 1796 after delegates had been sent to Vienna and Buda.