Merge "Multiple bugfixes, small restructuring." am: 26b614ddf6
am: 8883b4a06f

Change-Id: I5ec47936a2417d57a04eacd06cd8230e3eee9038