improve substitute_code_memo