Problem 4 and refactoring