Refactor and add docs