loop function

Calculates the fence and the loop