Factor common codes that show up in multiple places. Change-Id: I0a72213a151f74bdad926d59f86f0a28d00968fc