add set cookie function, and clean the code