Docstrings and improved algorithm