Implemented solution to problem 2