Updated with error handling