Some refactoring and comments