Improved state detection.
Merge branch 'test' into dev