Refactorizing exercise 7 from week 3