build tail logic