refactor to unwind loops for integrating with framework/executor