Attempted solution for Problem 5