Working brute force solution