Better error handling and logging