fix some errors and refactoring