This is a general function. May be it should be moved somewhere else, so that others can use it as well.