Added log, did refactoring of code