return (a->pos > b->pos) ? 1 : -1;
 }
 
-static bool
-checkcondition_QueryOperand(void *checkval, QueryOperand *val)
+typedef struct 
 {
-       return (bool) (val->istrue);
-}
+       TSQuery         query;
+       bool       *operandexist;
+} QueryRepresentation;
 
-static void
-reset_istrue_flag(TSQuery query)
-{
-       QueryItem  *item = GETQUERY(query);
-       int                     i;
+#define        QR_GET_OPERAND_EXISTS(q, v)     ( (q)->operandexist[ ((QueryItem*)(v)) - GETQUERY((q)->query) ] )
+#define QR_SET_OPERAND_EXISTS(q, v)     QR_GET_OPERAND_EXISTS(q,v) = true
 
-       /* reset istrue flag */
-       for (i = 0; i < query->size; i++)
-       {
-               if (item->type == QI_VAL)
-                       item->operand.istrue = 0;
-               item++;
-       }
+static bool
+checkcondition_QueryOperand(void *checkval, QueryOperand *val)
+{
+       QueryRepresentation *qr = (QueryRepresentation*)checkval;
+       return QR_GET_OPERAND_EXISTS(qr, val);
 }
 
 typedef struct
 
 
 static bool
-Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
+Cover(DocRepresentation *doc, int len, QueryRepresentation  *qr, Extention *ext)
 {
        DocRepresentation *ptr;
        int                     lastpos = ext->pos;
         * (though any decent compiler will optimize away the tail-recursion.   */
        check_stack_depth();
 
-       reset_istrue_flag(query);
+       memset( qr->operandexist, 0, sizeof(bool)*qr->query->size );
 
        ext->p = 0x7fffffff;
        ext->q = 0;
                for (i = 0; i < ptr->nitem; i++)
                {
                        if(ptr->item[i]->type == QI_VAL)
-                               ptr->item[i]->operand.istrue = 1;
+                               QR_SET_OPERAND_EXISTS(qr, ptr->item[i]);
                }
-               if (TS_execute(GETQUERY(query), NULL, false, checkcondition_QueryOperand))
+               if (TS_execute(GETQUERY(qr->query), (void*)qr, false, checkcondition_QueryOperand))
                {
                        if (ptr->pos > ext->q)
                        {
        if (!found)
                return false;
 
-       reset_istrue_flag(query);
+       memset( qr->operandexist, 0, sizeof(bool)*qr->query->size );
 
        ptr = doc + lastpos;
 
        {
                for (i = 0; i < ptr->nitem; i++)
                        if(ptr->item[i]->type  == QI_VAL)
-                               ptr->item[i]->operand.istrue = 1;
-               if (TS_execute(GETQUERY(query), NULL, true, checkcondition_QueryOperand))
+                               QR_SET_OPERAND_EXISTS(qr, ptr->item[i]);
+               if (TS_execute(GETQUERY(qr->query), (void*)qr, true, checkcondition_QueryOperand))
                {
                        if (ptr->pos < ext->p)
                        {
        }
 
        ext->pos++;
-       return Cover(doc, len, query, ext);
+       return Cover(doc, len, qr, ext);
 }
 
 static DocRepresentation *
-get_docrep(TSVector txt, TSQuery query, int *doclen)
+get_docrep(TSVector txt, QueryRepresentation *qr, int *doclen)
 {
-       QueryItem  *item = GETQUERY(query);
+       QueryItem  *item = GETQUERY(qr->query);
        WordEntry  *entry;
        WordEntryPos *post;
        int4            dimt,
                                j,
                                i;
-       int                     len = query->size * 4,
+       int                     len = qr->query->size * 4,
                                cur = 0;
        DocRepresentation *doc;
        char       *operand;
 
        doc = (DocRepresentation *) palloc(sizeof(DocRepresentation) * len);
-       operand = GETOPERAND(query);
-       reset_istrue_flag(query);
+       operand = GETOPERAND(qr->query);
 
-       for (i = 0; i < query->size; i++)
+       for (i = 0; i < qr->query->size; i++)
        {
                QueryOperand *curoperand;
 
                
                curoperand = &item[i].operand;
                
-               if(item[i].operand.istrue)
+               if(QR_GET_OPERAND_EXISTS(qr, &item[i]))
                        continue;
 
-               entry = find_wordentry(txt, query, curoperand);
+               entry = find_wordentry(txt, qr->query, curoperand);
                if (!entry)
                        continue;
 
                                int                     k;
 
                                doc[cur].nitem = 0;
-                               doc[cur].item = (QueryItem **) palloc(sizeof(QueryItem *) * query->size);
+                               doc[cur].item = (QueryItem **) palloc(sizeof(QueryItem *) * qr->query->size);
 
-                               for (k = 0; k < query->size; k++)
+                               for (k = 0; k < qr->query->size; k++)
                                {
                                        QueryOperand *kptr = &item[k].operand;
                                        QueryOperand *iptr = &item[i].operand;
                                                /* if k == i, we've already checked above that it's type == Q_VAL */
                                                doc[cur].item[doc[cur].nitem] = item + k;
                                                doc[cur].nitem++;
-                                               item[k].operand.istrue = 1;
+                                               QR_SET_OPERAND_EXISTS( qr, item+k );
                                        }
                                }
                        }
                                PrevExtPos = 0.0,
                                CurExtPos = 0.0;
        int                     NExtent = 0;
+       QueryRepresentation     qr;
+
 
        for (i = 0; i < lengthof(weights); i++)
        {
                invws[i] = 1.0 / invws[i];
        }
 
-       doc = get_docrep(txt, query, &doclen);
+       qr.query = query;
+       qr.operandexist = (int*)palloc0(sizeof(bool) * query->size);
+
+       doc = get_docrep(txt, &qr, &doclen);
        if (!doc)
+       {
+               pfree( qr.operandexist );
                return 0.0;
+       }
 
        MemSet(&ext, 0, sizeof(Extention));
-       while (Cover(doc, doclen, query, &ext))
+       while (Cover(doc, doclen, &qr, &ext))
        {
                double          Cpos = 0.0;
                double          InvSum = 0.0;
 
        pfree(doc);
 
+       pfree( qr.operandexist );
+
        return (float4) Wdoc;
 }
 
 {
        ArrayType  *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        TSVector        txt = PG_GETARG_TSVECTOR(1);
-       TSQuery         query = PG_GETARG_TSQUERY_COPY(2); /* copy because we modify the istrue-flag */
+       TSQuery         query = PG_GETARG_TSQUERY(2);
        int                     method = PG_GETARG_INT32(3);
        float           res;
 
 {
        ArrayType  *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        TSVector        txt = PG_GETARG_TSVECTOR(1);
-       TSQuery         query = PG_GETARG_TSQUERY_COPY(2); /* copy because we modify the istrue-flag */
+       TSQuery         query = PG_GETARG_TSQUERY(2);
        float           res;
 
        res = calc_rank_cd(getWeights(win), txt, query, DEF_NORM_METHOD);
 ts_rankcd_ttf(PG_FUNCTION_ARGS)
 {
        TSVector        txt = PG_GETARG_TSVECTOR(0);
-       TSQuery         query = PG_GETARG_TSQUERY_COPY(1); /* copy because we modify the istrue-flag */
+       TSQuery         query = PG_GETARG_TSQUERY(1);
        int                     method = PG_GETARG_INT32(2);
        float           res;
 
 ts_rankcd_tt(PG_FUNCTION_ARGS)
 {
        TSVector        txt = PG_GETARG_TSVECTOR(0);
-       TSQuery         query = PG_GETARG_TSQUERY_COPY(1); /* copy because we modify the istrue-flag */
+       TSQuery         query = PG_GETARG_TSQUERY(1);
        float           res;
 
        res = calc_rank_cd(getWeights(NULL), txt, query, DEF_NORM_METHOD);