refactor and paging bugfix