Refactoring and add more comments