Refactored duplicate code for save and update