Fixed bug where cursor was still being used in query