Corrected and tested algorithms.