Refactor cursor and iterator