Fixes on variable replacement