fixed a bug with the loop, now does not execute first element twice in a row