Refactoring do and for loop