In the 19th century, Afghanistan was seen as an important buffer state to the north-west of British-ruled India. In 1866 Sher Ali Khan came to power and was initially well disposed towards Britain. During the next 10 years, relations between the two countries deteriorated, primarily over the issue of Russian encroachment on Afghanistan. In 1878, Sher Ali reluctantly allowed a Russian mission to Kabul, and refused entry to the Viceroy Lord Lytton. After this refusal, Britain sent him an ultimatum that demanded a British envoy be accepted into Afghanistan; when this was ignored, Britain sent in three columns of British troops. The three British columns proceeded over the Bolan Pass to Kandahar, the Khyber Pass to Ali Masjid and through the Kurram Valley to Kabul. After several large victories for the British in 1878, fighting continued in the harsh mountainous terrain through the early months of 1879. As the British marched on Kabul, Sher Ali fled, leaving Yakub Khan to sign the Treaty of Gandamak on 26 May 1879 which required a British envoy in Kabul and the relinquishing of foreign affairs to the British. When the Afghan army mutinied in late 1879, Frederick Roberts, 1st Earl Roberts launched punitive actions and he occupied Kabul on 6 October 1879. After a popular uprising in December, Roberts withdrew to Sherpur where they were besieged for three weeks before launching a major attack on 22-23 December where they returned to Kabul and occupied it once again. Abdur Rahman Khan was instated as Emir in July 1880 but Ayub Khan led a rebel force which defeated the British at the Battle of Maiwand and besieged Kandahar. Roberts led a force from Kabul to Kandahar that defeated the rebels at the Battle of Kandahar on 1 September 1880. British forces withdrew in 1887 after Abdur Khan confirmed the initial Treaty of Gandamak and Britain's control over foreign policy.