Code for problem 2