Refactored internal device state machine