Fix NPE and a BytesRef ownership issue.