Reorganized and started error handling