added flag to prevent duplicate newline