Added error handling and logging