Fix possible memory overflow and remove DEBUG_MAX_BUFFER