The areas of the Baltic states have seen different regional and imperial affiliations during their existence. They were first included under the same political entity when the Russian Empire expanded in the 18th century. The territories of Estonia and Latvia were joined into the Russian Empire at the end of the Great Northern War in 1721, while the territory of Lithuania came under the Russian rule after the Third Partition of Poland in 1795. The territories of the Baltic states were ruled by the Russian Empire until the end of the World War I when Estonia, Latvia and Lithuania gained their sovereignty. They remained independent until the occupation and annexation by the Soviet Union and briefly, Nazi Germany during World War II before the Soviets regained control of the Baltic states. Soviet rule ended when the three countries declared the occupation illegal and culminated with the restoration of independence to their pre-war status in 1990–1991 when the communist rule collapsed in Eastern Europe.