Move two funcs to util. They are used more than once.