Made the common functionality between flipx and flipy in a single function.