Reworked loop structure.
Fixed break condition