fixed loop counter to be more flexible