Explicitly close cursors

Updated the codebase to explicitly close cursors when they are no longer needed, instead of relying upon ``__del__`` and garbage collection.