cached scanning and minor refactoring