Made more modular after rearranging a lot and switching from FILE *s to file descriptors.