refactoring and adding better comments