Your specific problem is that the S5() call is inside the outer for loop (it needs to be outside that loop).
The problem is much easier to see if you indent your code... that's a good learning. I personally also like to keep the { and } in line so it is very clear what is being executed in that block/loop.