Updated with more robust error management