full solution to the exercise