Handles Cursor problems

Refactored logic with functional decomposition and updated logic so that
we do not run into open cursor errors.