Most of the time is taken up in that loop. I optimized it and rewrote it in assembly.