Handle trailing newlines appropriately