Fix optimization of nested switches.

Also make switchify's implementation more elegant.