Correct implementation of problem 24.