updating with better error handling