Refactored code and added the update function