I hate this algorithm.