adding NULL pointer protection and safe string operation