move uninit and fix some mem leaks
Fix incorrect free() and refactor enum_formats
More robust init condition checks