Although some institutions, such as the National Institute and the National Library, were installed in the Patria Vieja, they were closed after the patriot defeat at the Battle of Rancagua in 1814. The royal government lasted until 1817, when the Army of the Andes secured victory in battle of Chacabuco, reinstating the patriot government in Santiago. Independence, however, was not assured. The Spanish army gained new victories in 1818 and headed for Santiago, but their march was definitively halted on the plains of the Maipo River, during the Battle of Maipú on 5 April 1818.