Fixed a bug that could sometimes result in no output file being generated.
Also, removed the use of some more modern bash features. This should make it
easier to run the script on older systems.