refactored loop