working version of scan .
 modifications here and there nothing worth noting