Factored this code into a utility function.