San Martín proclaiming the independence of Peru. Painting by leftThe Viceroy of Peru, Joaquín de la Pazuela named José de la Serna commander-in-chief of the loyalist army to protect Lima from the threatened invasion by San Martin. On 29 January, de la Serna organized a coup against de la Pazuela, which was recognized by Spain and he was named Viceroy of Peru. This internal power struggle contributed to the success of the liberating army. To avoid a military confrontation, San Martin met the newly appointed viceroy, José de la Serna, and proposed to create a constitutional monarchy, a proposal that was turned down. De la Serna abandoned the city, and on 12 July 1821, San Martin occupied Lima and declared Peruvian independence on 28 July 1821. He created the first Peruvian flag. Upper Peru (Bolivia) remained as a Spanish stronghold until the army of Simón Bolívar liberated it three years later. José de San Martin was declared Protector of Peru. Peruvian national identity was forged during this period, as Bolivarian projects for a Latin American Confederation floundered and a union with Bolivia proved ephemeral.