added elements valid guard and cleaned up