big speed improvements to script