refactored common code of of add/update