More and more bug fixes as unit tests are written.

Signed-off-by: Ben Corlett <bencorlett@me.com>