implemented chain detection logic, removal logic, and creation of new cookie logic
detect matches after every rearrangement