When Gen. Joseph Johnston, CSA was wounded at Fair Oaks, President Davis replaced him with General Lee. Richmond was threatened but Lee struck before McClellan could break through.
By the first of July, 1862 McClellan had the Army of the Potomac on a commanding plateau at Malvern Hill. At first Lee thought McClellan's position was too strong to assault, but a shifting of Federal troops deceived him into changing his mind and attacking. This proved imprudent and things went badly.
The Confederate formations were shattered, costing Lee 5,500 men. On the following day, Lee ordered his men back to Richmond.His attacks, while costly, had saved Richmond for the Confederacy.
The Peninsula Campaign cost the Union Army 15,849 men, but the Confederates lost more by doing most of the attacking: 20,614. Improvements in the training and discipline of both armies since the disorganized fighting at Manassas was notable. But also significant was the fact that higher commanders had not thoroughly mastered their jobs. Way too many wasted opportunities on both sides.
An interesting book to read on the subject is Extraordinary Circumstances: The Seven Days Battles