Cleaned up code, added output file name as an argument.