refactoring scanner into state pattern