improve logging and mail_handler for errors