refactored loops