refactor switches to eliminate a lot of duplicate code