fixed double declaration of var in loop