Add better error handling and logging