fix mistakes and refactoring