This function was written incorrectly and would almost always trigger an infinite loop. It's now written correctly and achieves what's intended.