Refactor, document, test
Add documentation