Refactored solution 16 and added test case.