This function looks like it should go into a util library...