Refactoring and remove debug messages