Updated pagination and sort logic