fixed loop counter bug