some refactoring
more refactoring and fix the case when the yield function errors out
more cleanup