refactored pagination