refactorize pagination