Add documentation and some code improvement