Add test cases and defect fixing