Close cursor properly