Refactor some shared util functions.