code changes for pagination and use TreeMap instead of HashMap to maintain the ordering