Improved performance by rewriting key functions in C. Fixed a bug in the iterator function.