Bug fix.
Fixed two boundary cases where infinite loops can occur