updated with code for problem 6