Moved 'global' variables to main loop