This is probably too much logic in one construct -- building it up via temp variables would be cleaner I think. (But it's not worth the candle, so I'll accept the PR anyway...)