fixed all errors and refactor