Extracted updateXY to a function