Isn't there such a function somewhere already or could we use this in other places? Seems like a common task.