Large refactor of Scanner.