Moved var out of loop