Refactored solution 9 and added a test case.