In the late 9th century, Silla was divided into the Later Three Kingdoms (892–936), which ended with the unification by Wang Geon's Goryeo dynasty. Meanwhile, Balhae fell after invasions by the Khitan Liao dynasty and the refugees including the last crown prince emigrated to Goryeo, where the crown prince was warmly welcomed and included into the ruling family by Wang Geon, thus unifying the two successor states of Goguryeo. During the Goryeo period, laws were codified, a civil service system was introduced, and culture influenced by Buddhism flourished. However, Mongol invasions in the 13th century brought Goryeo under its influence until the mid-14th century.