Refactoring and adding comments