Fixed memory leaks and resource (file handle) leaks
Avoid forcing the content-type to text/plain if its already determined