Refactored exercise 4