Refactoring and adding a few more comments