Some refactoring to remove unbounded recursion.
More work related to Task monad and some fixes for bad recursion.