Refactor #replace_file