holy fuck this algorithm, like balls