Some minor refactoring and more comments