Closing cursor when a query is complete