I think this should be a utility function (as you do the exact same processing elsewhere).