Code rearrangement
More robust state polling.