Fixed the flip function