State Scanner

Scan using state functions instead of a morass of nested conditionals.