Adding some guarding of undefined objects to remove IE8 errors.