         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
+          IF rev.tstamp IS NULL OR last_way.version != rev.version OR (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;
+          ELSE
+            --raise notice 'not adding 3 % %', rev_geom, rev.geom;
           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;
+
+        IF last_row.type IS NOT NULL AND last_row.type = 'W' THEN
+          rev := ($1,last_row.version,rev_count,last_row.visible,last_row.user_id,last_row.tstamp,last_row.changeset_id,OWL_MakeLineFromTmpNodes(last_row.nodes));
+          rev_count := rev_count + 1;
+          created_count := created_count + 1;
+          RETURN NEXT rev;
+          --raise notice 'added 4 %', rev;
         END IF;
       END IF;
+    END IF;
+
+    IF row.type = 'W' THEN
+      last_way := row;
+      last_row := row;
+    ELSE
+      last_node := row;
+      last_row := row;
     END IF;
 
     last_changeset_id := row.changeset_id;

      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
    --RAISE NOTICE '%', row;

    IF row.type = 'N' THEN

      go := true;
    END IF;

    IF (row.type != 'END') AND row.visible AND ((NOT go) OR (last_way.nodes IS NOT NULL AND NOT last_way.nodes && row.nodes)) THEN
      CONTINUE;
    END IF;

    IF last_changeset_id IS NOT NULL AND row.changeset_id != last_changeset_id THEN
      IF last_node.tstamp IS NOT NULL THEN
        rev_geom := OWL_MakeLineFromTmpNodes(last_way.nodes);

        IF rev.tstamp IS NULL OR last_way.version != rev.version OR (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 1 %', rev;
        --ELSE
          --raise notice 'not adding 1 % %', rev_geom, rev.geom;
        END IF;
        SELECT NULL::timestamp without time zone AS tstamp INTO last_node;
      ELSIF last_way.changeset_id = last_changeset_id THEN
        rev := ($1,last_way.version,rev_count,last_way.visible,last_way.user_id,last_way.tstamp,last_way.changeset_id,OWL_MakeLineFromTmpNodes(last_way.nodes));
        rev_count := rev_count + 1;
