multithreaded

rebased to v1.2, ignore the 'binary search for shape optimization', that was another branch