refactor pagination query