Amazingly slow algorithm, works for the example but to solve the problem it needs to be a whole lot more efficient