Clean up internals

Just a bit of refactoring on the internals - trying to make custom query cursors a reality.