code improvements and fix minor issues in test cases