Rearranged code

cookies needed to be set at the beginning