Corrected errors handling, added mime type detection