refactor and document a bit