Lee took command of the Army of Northern Virginia in June 1862 during the Peninsula Campaign following the wounding of Joseph E. Johnston. He succeeded in driving the Union Army of the Potomac under George B. McClellan away from the Confederate capital of Richmond during the Seven Days Battles, although he was unable to destroy McClellan's army. Lee then overcame Union forces under John Pope at the Second Battle of Bull Run in August. His invasion of Maryland that September ended with the inconclusive Battle of Antietam, after which he retreated to Virginia. Lee then won two decisive victories at Fredericksburg and Chancellorsville before launching a second invasion of the North in the summer of 1863, where he was decisively defeated at the Battle of Gettysburg by the Army of the Potomac under George Meade. He led his army in the minor and inconclusive Bristoe Campaign that fall before General Ulysses S. Grant took command of Union armies in the spring of 1864. Grant engaged Lee's army in bloody but inconclusive battles at the Wilderness and Spotsylvania before the lengthy Siege of Petersburg, which was followed in April 1865 by the capture of Richmond and the destruction of most of Lee's army, which he finally surrendered to Grant at Appomattox Court House.