Refactored Cookie Detection Code