Refactored solution 30 and added a test case.