     __device__ inline
     const int2& get( Direction d )
     {
-        return edge_buffer[d][edge_buffer_head[d]];
+        return edge_buffer[edge_index[d]];
     }
 
     __device__ inline
     void append( Direction d, int2 val )
     {
-        edge_buffer_head[d]++;
-        edge_buffer[d][edge_buffer_head[d]] = val;
+        if( d == Left ) {
+            edge_buffer[edge_index[Left]] = val;
+            inc( edge_index[Left] );
+            assert( edge_index[Left] != edge_index[Right] );
+        } else {
+            dec( edge_index[Right] );
+            edge_buffer[edge_index[Right]] = val;
+            assert( edge_index[Left] != edge_index[Right] );
+        }
     }
 
     __device__ inline
     int size() const
     {
-        return edge_buffer_head[Left] + edge_buffer_head[Right] + 1;
+        if( edge_index[Left] > edge_index[Right] ) {
+            return edge_index[Left] - edge_index[Right];
+        } else {
+            return edge_index[Left] + ( EDGE_LINKING_MAX_EDGE_LENGTH - edge_index[Right] );
+        }
     }
 
     __device__

        // This direction still has points.
        // We can identify the highest threadId / lowest rotation value j
        uint32_t computer = __ffs( any_point_found ) - 1;
        if( computer >= 8 ) {
            printf("point found by thread %d\n", computer);
            assert( computer < 8 );
        }

        found = LOW_FLOW;


        }
        int j = 0;
        int2* ptr = output.ptr(idx);
        for( int idx=edge_index[Right]; idx!=edge_index[Left]; inc(idx) ) {
            ptr[j] = edge_buffer[idx];
            j++;
        }
    }

private:
    __device__ inline void inc( size_t& idx )
    {
        idx = ( idx >= EDGE_LINKING_MAX_EDGE_LENGTH-1 ) ? 0 : idx + 1;
    }

    __device__ inline void dec( size_t& idx )
    {
        idx = ( idx == 0 ) ? EDGE_LINKING_MAX_EDGE_LENGTH-1 : idx - 1;
    }
};

