Refactored ANSI replacement code, now that I know what works.