Reordered the writing of the input and output.
Note that last value now discarded.