wrote pivot logic