Refactor update code into functions