Refactor replace_vars: some more small improvements