scanner: fix bugs and tests