implementation with head and tail