added element checking and invalid alerting