refactor implementation of runAGDag