Added some comments and minor refactoring.