Increased correctness of brute force algorithm