There is actually a function that does this. I have a program that does this somewhere so I'll try to get back to you on this one.