Brute force algorithm