refactor paging code