Add some unit tests and fix a bug.