Restructure algorithms.

Thanks to Martin for his feedback! [resulted in the latest commits]