Implementing pagination logic