Fix for "TypeError: Cannot read property 'type' of undefined"

Alternative fix for #4