refactored 'loop' method