improve scanning and added debug messages for scanning