Refactoring of method and correct the output