first implementation of problem 22