Code refactoring and adding comments