refactored exercise 3 code