Refactored create and update functions