updated with error handling