fixed error on loop breaking before assigning to variable