Some code refactor. Docstrings.