refactoring
added global error handler
improved global handler