Moved state machine to isr