Added notes and beautified the code