Fixed a bunch of state machine-related issues.