Fixed race condition with cursors