refactor to use cursor