Added a more cursor-like approach for querying data, to be more memory efficient.