Refactor set_cookie function