Some minor refactoring and comments added.