refactoring the update function