Added text/plain to list of valid mimes, refactored code