This code snippet exists in enough places that it should really be in a function.