Refactor some function out to utils