Implemented logic for flipping and unflipping