Added documentation and clarified the code a little bit