The only point during this period when the situation became hopeful for Leon was the reign of Ramiro II. King Ramiro, in alliance with Fernán González of Castile and his retinue of "caballeros villanos", defeated the Caliph in Simancas in 939. After this battle, when the Caliph barely escaped with his guard and the rest of the army was destroyed, King Ramiro obtained 12 years of peace, but he had to give González the independence of Castile as payment for his help in the battle. After this defeat, Moorish attacks abated until Almanzor began his campaigns. Alfonso V finally regained control over his domains in 1002. Navarre, though attacked by Almanzor, remained intact.