exercise 2, plus some minor changes