Does anyone with greater knowledge see flaws with this idea?Yes. There are cases where the insanity defense is clearly correct, and extended incarceration makes no sense. One example is a man who had become very abusive to his wife and newborn. Medical tests demonstrated the existence of a brain tumor. Removal of the tumor removed his uncontrollable rages and restored his normal state of being. (Along with great remorse over what he had done.) After that, what purpose would incarceration have served other than to derive his wife and young child of much needed income?
In practice, though, I've heard that those who are found to be insane will, on average, wind up incarcerated longer than those who are not. True, it is a mental hospital rather than a jail. But the effect is similar to what you indicate. With the twist that discharge happens only after medical professionals judge that the person has improved, rather than having the discharge happen according to a guess by the judge.