Fixed file type detection, added error messages