extract the switch block in it's own function