Code cleanup + testcases