optimize token replacement