It looks like you can just remove check for undefined and leave check for function.