partial newline handling
removed test code
optimizations: reduce reliance on sed