Refactor common functionality, document, test