Some semi-working version of algorithm.