Fix format bug and add tests.
Fixes #146