started refactoring save function