         if (card.isTreasure) and (not (card in treasures))
           treasures.push card
             
-      for card in treasures
+      for treasure in treasures
         [hypState, hypMy] = state.hypothetical(my.ai)
-        hypMy.doDiscard(card)
+        hypMy.doDiscard(treasure)
         nowGained = hypMy.ai.pessimisticCardsGained(hypState)
-        state.log("card #{card}, HypGaines: #{nowGained}")
         # test arrays on equality. Better way?
         # changed order in gaining messes this up. Just ignore?
         equal = true

      [hypState, hypMy] = state.hypothetical(my.ai)
      # technically, we don't want to discard but to put back, but for pessimisticBuy this should not matter
      dis = hypState.requireDiscard(hypMy, 1)
      state.log("Discarded: #{dis}")
      defaultGained = hypMy.ai.pessimisticCardsGained(hypState)
      state.log("Defaultgain: #{defaultGained}")
      
      # determine which treasures are in hand, discard them hypothetically and test if the gains change.  When it doesn't, we can put back this card.
      treasures = []

            equal = (equal and (nowGained[index] == card))
        
        if (equal)
          putBack.push treasure
        
      
      # sort by cost. Should be improved
      putBack.sort( (y,x) -> x.getCost(state)[0] - y.getCost(state)[0] )
      
      # Don't put back last Potion if Alchemists are in play
      if my.countInPlay(state.cardInfo["Alchemist"])>0
        if my.countInHand(state.cardInfo["Potion"]) == 1
          putBack.remove(state.cardInfo["Potion"])
      
      # Don'tput back Royal Seal if you bought treasures or actions, assuming that you want to play them asap if you take the effort to buy them
      onlyVictoryBought = true
      for card in defaultGained
        onlyVictoryBought = (onlyVictoryBought and not (card.isTreasure or card.isAction))
        
      if not onlyVictoryBought
        putBack.remove(state.cardInfo["Royal Seal"])
      
      # Think about Loan?
    
    # 4) Put back the worst card (take priority for discard)
    #
