fix errors refactor code