First working version of all 3 algorithms.