Fix bug. Add test cases.