improvements to scanning algo