optimizing count_violations