Refactor scan() into test_scan() and scan()