inserted typeof check for "undefined"