a vast number of muslim refugees from shaanxi fled to gansu. some of them formed the "eighteen great battalions" in eastern gansu, intending to fight their way back to their homes in shaanxi. while the hui rebels were preparing to attack gansu and shaanxi, yaqub beg, who had fled from kokand khanate in 1865 or 1866 after losing tashkent to the russians, declared himself ruler of kashgar and soon managed to take complete control of xinjiang.