Refactored UserName to handle NULL case and fixed associated errors