Moved variables outside the loop