Fixed some loops and counters