runs very fast, but hangs for values of limit larger than ~22000