Refactor and documentation