Fix elementary errors in loop