refactor createPagination function