Added some comments, refactored