Messed up loop fixed