During the early 19th century, Spain underwent great changes due to the industrial revolution. Modern industries developed thanks to inventions like the steam engine, bringing electricity to cities, and steam locomotives. Cities changed dramatically with wider streets, new buildings like schools and hospitals, and population growth due to improved healthcare. However, this also increased inequality between social classes. The first king of 19th century Spain was Carlos IV, until France invaded in 1808 under Napoleon, starting the War of Independence where Spanish guerrillas and allies helped defeat French forces. Political instability in early 20th century Spain included the loss of its last colonies and the rise and fall of the Second Spanish Republic, leading to a civil war.