added valid check for main loop