improved problem 52 solution