Exercise 2.
Some refactoring.