Almost working implementation of problem 50