Clean up the ugly switch into a function