added error handling and logging