Lots of small fixes to fix piping to file not working correctly due to unicode.