minor refactoring and better comments