better error handling and logging