Brought in code from TTAPI