Clean newlines from output and minor refactoring.