Updated the truncate function to be smarter