Working version. Testcases need to be fixed.