refactored model save code