refactored code to use paging