During the reigns of Muhammad IV (1859–1873) and Hassan I (1873–1894), the Alaouites tried to foster trade links, especially with European countries and the United States. The army and administration were also modernized to consolidate control over the Berber and Bedouin tribes. In 1859, Morocco went to war with Spain. The independence of Morocco was guaranteed at the Conference of Madrid in 1880, with France also gaining significant influence over Morocco. Germany attempted to counter the growing French influence, leading to the First Moroccan Crisis of 1905–1906, and the Second Moroccan Crisis of 1911. Morocco became a French protectorate through the Treaty of Fez in 1912.