works for more complicated zig-zag case