The above code looks like it was intended to already catch this case - can you clarify why it did not do so in your case?

In principle, the change looks reasonable (the current code seems unnecessarily complex and full of magic numbers) but I'd like to make sure the change doesn't miss some odd corner case that the old code handled before committing. I'll do a fuller review soon.