Better brute force solution