Tidied up cursor and connection code and improved support for concurrent query execution