Make sure we don't run off the end of the document if it's now shorter