Added code to write bins to output files