Adding rudimentary speed testing and a few optimizations in the inner loop