Fix various output routing bugs and cleaned up state creation code