#8: Fixed few algorithm bugs, added docstrings.