Now the stuff works and the algorithm is super efficient