Use type switch in merge functions

Followup PR from #262. 

* Use a type switch rather than a type assertion when checking for unexpected values in merge functions
* Drive by code cleanups.