Refactor scan() into a switch/case.