Fix and add test cases