Working implementation with test case.