only cleanup ctx if we need to, save ctx flags when we do