refactoring, added the loop