Improve nameId sanitization function