Refactored code for Pagination