updated loops with new code