fix switch priority errors
factor out state-machine for core