pagination refactored and tested