This packs a lot of conditionals into a single line. This is a result of the surrounding code, but it would be good to refactor this so it's a bit cleaner before we merge it in.