Refactored solution 4 and simplified testing.