Close cursor and cleanup