Refactor replace_vars: Move some more code to it's new place
Refactor replace_vars: build in register_var_replacement functionality
Refactor replace_vars: Move yet more code to it's new place