       ARRAY[]::bigint[],
       NULL AS geom) q
   WHERE last_rev.tstamp IS NULL OR q.tstamp > last_rev.tstamp
-  ORDER BY q.tstamp NULLS LAST, q.type DESC) LOOP
+  ORDER BY q.tstamp NULLS LAST, q.type) LOOP
     --RAISE NOTICE '%', row;
 
     IF row.type = 'N' THEN

        RETURN NEXT rev;
        --raise notice 'added 2 %', rev;
      END IF;

      IF row.type = 'W' THEN
        last_way := row;
      ELSE
        IF last_way.nodes && row.nodes THEN
          last_node := row;
        END IF;
      END IF;
    ELSE
      IF row.type = 'W' THEN
        IF last_node.tstamp IS NOT NULL THEN
          rev_geom := OWL_MakeLineFromTmpNodes(last_way.nodes);
          IF rev_geom IS NULL OR (rev_geom IS NOT NULL AND rev.geom IS NULL) OR (rev.geom IS NOT NULL AND rev_geom IS NOT NULL AND NOT rev.geom = rev_geom) THEN
            rev := ($1,last_way.version,rev_count,last_way.visible,last_node.user_id,last_node.tstamp,last_node.changeset_id,rev_geom);
            rev_count := rev_count + 1;
            created_count := created_count + 1;
            RETURN NEXT rev;
            --raise notice 'added 3 %', rev;
          END IF;
        END IF;
        SELECT NULL::timestamp without time zone AS tstamp INTO last_node;
        last_way := row;
      ELSE
        IF last_way.nodes && row.nodes THEN
          last_node := row;
        END IF;
      END IF;
    END IF;

    last_changeset_id := row.changeset_id;

  last_way record;
  last_rev way_revisions%rowtype;
  last_inserted_rev way_revisions%rowtype;
  last_row record;
  rev way_revisions%rowtype;
  rev_count int;
  created_count int;
