Fixed inversion count logic