better implementation of loop-until-done function