This and the state setting logic look reasonable.  I will try to test locally.