Some fixes on error handling