fixed logic control bug and wait instead of sleep