Fixed bug in scan routine