Refactoring to do pagination and filtering