Refactored cookie functions