clean pattern and bounds
rename some names
tweak
tidy up
tweak the scanning of pattern variables
add variable bounds checking