bug fix in loop