an almost not hanging threaded version, that runs and passes tests