Fixed bug in trim_tail function