moving common code into function