improve pagination logic