Updated internal error handler

Updated internal error handler to better handle errors and added a
feature to specify in config an email address where to send errors in
case the debug mode is off (i.e. running on production).