Improved error handler
Various fixes and improvements