Fix some issues with XMP, fileno() usage
Fix a whole bunch of memory leaks (thanks again, Valgrind)