Beginning of the scan function 
+ pseudo code