refactored into loop