I restructured this function trying to get a structure that was really easy to read and reason about with as few nested ifs as possible. I ended up with this. It still has that weird fxos special case, but I'm not sure there's anything we can do about that now.