In the Middle Ages, most of the present territory of Moldova was part of the Principality of Moldavia. In 1812, it was annexed by the Russian Empire, and became known as Bessarabia. Between 1856 and 1878, one of the eight counties was returned to Moldavia, which in 1859 united with Wallachia to form modern Romania. Upon the dissolution of the Russian Empire in 1917, an autonomous, then independent Moldavian Democratic Republic was formed, which joined Romania in 1918. In 1940, Bessarabia was occupied by the Soviet Union and was split between the Ukrainian & the newly-created Moldavian SSR.