Refactoring to use consistent loops