Fix crash replacing lexical references

Change-Id: Ia44e8eb786bd83b19379748a0c2600cf6371e9e9