updated the wrong variable in the loop