Some minor refactoring and comments