Thought of a better solution to problem 5