Added a few safety checks around state access.