minimal refactoring and comments