trim() used, improved code to write files