Closing cursors in order to avoid the exceptions for memory and opening cursors