Rewrote it in C because reasons