Around the early 16th century, following the construction of the Ribeira Palace beside the Tagus river, the Palace of Alcáçova began to lose its importance. An earthquake occurring in 1531 further damaged the old castle, contributing further to its decay and neglect. In 1569, King Sebastian ordered the rebuilding of the royal apartments in the castle, intending to use it as his official residence. As part of the rebuilding, in 1577 Filippo Terzi demolished one of the towers near the principal facade of the Church of Loreto. However, many of the works were never completed after the young king's apparent death during the Battle of Alcácer Quibir. The following Portuguese dynastic crisis opened the way for sixty years of Spanish rule and the castle was converted into military barracks and a prison. On 30 December 1642, Teodósio de Frias the Younger was appointed master builder to continue the works begun by his father, Luís de Frias, and his grandfather, Teodósio de Frias. This was part of a greater plan by the Spanish forces to recommission the fortification.