refactoring and fixing