What about a `switch` on `mode` here? It will be (very slightly) more readable, IMHO.
(Yes, I'm nitpicking; it's just to prove that I actually reviewed the code ;-))