Code for Problem 6, works