Refactor replace_vars: Clean up more moved code and order methods alphabetically within their groups