move code from interrupt handler to state handler in main loop