moved state machine code outside of update code