Various bugfixes, re-enabled test cases and additional unit tests.

BUG=
R=scudette@gmail.com

Review URL: https://codereview.appspot.com/184960043