Adding robust error handling