The town of Loulan was abandoned in 330 CE, likely due to lack of water when the Tarim River, which supported the settlement, changed course; the military garrison was moved south to Haitou (海頭). The fort of Yingpan to the northwest remained under Chinese control until the Tang dynasty. According to the "Book of Wei", King Bilong of Shanshan fled to Qiemo together with half of his countrymen after an attack by Juqu Anzhou in 442 CE so Shanshan came to be ruled by Qiemo. In 445 Shanshan submitted to the Northern Wei. At the end of the 6th century, the Sui dynasty reestablished the city state of Shanshan.