tail call optimization for insertion