Improve perf by using type-switch instead of attempting a bunch of casts

Improve performance and reduce allocations by switching from an "if/else if/else if/else" block to a cleaner switch statement.