Minor refactorings and added an ANSI escape-aware output stream