Added -o flag to specify output file, and fixed file saving.