Attempted solution for Problem 4