This function looks like a candidate for the utils module.