Error handling to prevent crashes