Corrected Problem 3 code