Refactor some function