 In 1165, he granted the custody of the city to a knights' order of the church, first to the Order of Knights Templar, and later to the Order of Santiago.The city was given to the military orders to maintain the reconquered city with Christian hands, and Grand Master the Order of Knights Templar, Gualdim Pais, was manager of the building of the fortress.Later, King Sancho I of Portugal reconstructed and repopulated it after the wars with the Leonese.