add error handling and logging