Improve solution for exercise 8