Added some comments, did some minor refactoring.