First half of problem 147