Fix a bug in the zigzag algorithm