Generalized the pagination and ordering logic in the model