slow version of problem 4