[WIP] Fix exit

**DO NOT MERGE THIS**

This is highly WIP, it targets #493 (partly).