refactorred the update function to be legible and have fewer bugs