fix logic for cookie and add more debugging