added valid substring checking