More robust against exceptions and a few other fixes.