Added more documentation and refactoring some code