In the seventeenth century, a Tibetan expatriate Drukpa monk, Ngawang Namgyal established a theocratic government independent of political influence from Tibet proper, and premodern Bhutan emerged. Ngawang Namgyal arrived in Bhutan in 1616 seeking refuge from the domination of the Gelugpa sect led by the Dalai Lama in Lhasa. He established a new base in western Bhutan, founding Cheri Monastery at the head of Thimphu valley. In 1627 he built Simtokha Dzong at the entrance to Thimphu valley. From this dzong he exerted control over traffic between the powerful Paro valley to the west and Trongsa valley to the east. In the 1634 Battle of Five Lamas, Ngawang Namgyal prevailed over the Tibetan and Bhutanese forces allied against him and was the first to unite Bhutan into a single country. He took the title shabdrung, becoming the temporal and spiritual leader of Bhutan under a dual system of government. He promulgated a code of law, the Tsa Yig, and built a network of impregnable dzongs, a system that helped bring local lords under centralized control and strengthened the country against Tibetan invasions. Thus, more than 1,000 years of Tibetan influence and migration into the region brought about the formation of Bhutan.