With Ayub Khan defeated, the war was officially over and the British selected and supported a new Amir - Abdur Rahman Khan son of Muhammad Afzal and nephew of the former Amir Sher Ali. Rahman confirmed the Treaty of Gandamak, whereby the British took control of the territories ceded by Yaqub Khan, and also of Afghanistan's foreign policy in exchange for protection and a subsidy. The Afghan tribes maintained internal rule and local customs, and provided a continuing buffer between the British Raj and the Russian Empire.