optimized flip_rows