Tidier code
Correctly throw errors
Various fixes and improvements related to file type detection