Refactoring and fixing