Add some comments and minor refactoring.