Refactoring and debug