Added working code with states, but things freeze