Problem 9 solved but needs to be refactored