Added set_cookie() method and reduced duplicate code.