I think this function does what you want...