Added more documentation and comments to the code.