multi threaded version now supports an arbitrary number of threads