made debugging output optional
improved code regarding scanning