Refactoring and add logs.