added better state check