refactoring and fix issues