refactored for loop with guard