Refactor some functions