massive performance improvements

thanks to Mek for the advice here