added some VERY rough threading. Still need to optimize, though.