refactor code, add docs