refactoring + save file function