fix a bug in cookie loop processing, add debug logging