some code refactoring, add docs