Improved error handling

Script should terminate immediately after fatal errors to avoid
unexpected side effects.