Completed insertNewlines function