Battle of Ayacucho own. The royalists in each viceroyalty, however, took different paths. In New Spain, royalists, after defeating the insurgents, proclaimed a negotiated separation from Liberal Spain through the Plan of Iguala, which they negotiated with the remaining patriots, and the Treaty of Córdoba, which they negotiated with the new head of government, Juan O'Donojú. In Peru Viceroy Joaquín de la Pezuela was discredited after a royalist expedition to Chile under Mariano Osorio was defeated and advances in Peru were made by José de San Martín. The viceroy was overthrown on 29 January 1821, in Asnapukyu "(Aznapuquio)", in a coup by General
José Bernardo de Tagle y Portocarrero, Marquis of Torre Tagle member of the triumvirate under José de La Mar. In January 1823, the congress appointed him president, but a military mutiny deposed him and proclaimed José de la Riva Agüero president on 28 February. After the deposition of Riva Agüero and his retreat to Trujillo, Torre Tagle was appointed president by Antonio José de Sucre on 20 July, and elected by the congress on 16 August, and Bolivar, who on his arrival 1 September had been proclaimed dictator, left him in charge of the government. During his tenure, the Peruvian flag, which had been established by José de San Martín,