Properly declare variables inside of loops

Also suggested by Florent Hobein.