Code cleanup + improvement of test cases.