Update logic, fix bugs, refactor code style