Add writing output
Tidy ups, hook in output writing