Refactor some logics.