Add more debug logs and prevent Undefined index: data