Attempted solution for Problem 6