Document API, refactor, improve code quality + function