brute force algorithm works fine now