bug fix and test cases