Muhammad turned against Ibn Hud again in 1236. He allied himself with Ferdinand and helped the Castilians take Córdoba and end centuries of Muslim rule in the city. In the following years, Muhammad took control of important cities in the south. In May 1237 (Ramadan 634 AH), by invitation of the city's notables, he took Granada, which he then made his capital. He also took Almería in 1238 and Málaga in 1239. He did not take these southern cities by force, but through political maneuvering and the consent of the inhabitants.