more tweaks for performance
significantly, removed unintended global variables