Early's army surprised them at Kernstown on July 24, where McKinley came under heavy fire and the army was defeated. Retreating into Maryland, the army was reorganized again: Major General Philip Sheridan replaced Hunter, and McKinley, who had been promoted to captain after the battle, was transferred to General Crook's staff. By August, Early was retreating south in the valley, with Sheridan's army in pursuit. They fended off a Confederate assault at Berryville, where McKinley had a horse shot out from under him, and advanced to Opequon Creek, where they broke the enemy lines and pursued them farther south. They followed up the victory with another at Fisher's Hill on September 22 and were engaged once more at Cedar Creek on October 19. After initially falling back from the Confederate advance, McKinley helped to rally the troops and turn the tide of the battle.