refactored cookie logic