Add tests and fix some utility functions.