Working version of the non threaded version