Merge common application code.

Also fix some bugs along the way.

Change-Id: I010588ee771539d38b8ee49c0ae9c6937a49ca9a