Code cleanup and optimization

Change-Id: I3c92d98d18449c2f3a3bdf41b19f6295cfc77c66