First version of the loop function.