code refactoring and fixing