Corrected variable names where it'd been moved out of loop