Close cursors

Close SQL cursors to avoid leaking them
I think maybe we should rollback the changes for this file. If we want to have logging for open cursors etc we can do that at a later date.