Rewrote functionality in C. Same memfree issue as before.