Refactor state machine

Remove unnecessary state definitions.
Define scopes based on states dynamically.