refactored the looping