Thanks, yeah, actually removing return(ScanDist) seems to solve the issue for the most part.
However I've found a much better solution to my problem in the mean time. Instead of looking at pixels, I just take heights into account, certain textures (and thus pixel colors) are only found on lower areas,