added optional output file function and improved outputting results (no more writing and reading files)