reapply r85085 with a bugfix to avoid infinite looping.