Rewritten in C