Changed check for undefined values in off function