minor refactoring and adding comments