Solution Q9
More efficient Solution Q5