Fix bug, add test cases