Added support for paging

Added support for paging when iterating over an array. Fixed potential
bug with processing the queue.