Refactor cookie handling