rename ti_file to tiFile, update references
move mimetype to dictionaries, it's used multiple times
Move repetitive write loops to a function