final code version, estimated speed up 6.25x