Refactor cursor and add test case.