Performance improvments

I'm unsure how much you will want to deviate from the original API, but this change greatly improves performance.

From:

real    0m10.758s
user    0m10.739s
sys 0m0.017s

To:

real    0m2.198s
user    0m2.183s
sys 0m0.013s