Bug fixes and code cleaning.

Change-Id: I367a6fae1a67cd944fa9d452d53698780a602943