Refactoring, tests & docstrings