big performance improvement. reuse temp arrays where possible