Refactored source code and improved docstrings.

Docstring indentation now matches the code indentation. Also improved the unit tests.