partially working threaded version: runs out of memory