Bug fix. The element variable couldn't be found.
Not sure why the entity_type variable key didn't exist at times but this should correct for that error.
Corrected the conditions settings.
Corrected the variable key names.
Corrected the conditional check so that when no events are found it doesn't try to loop through all events.