Rewrite error handling and deinitialization in dispatcher

I forgot a bit when I wrote my last patch.