Add tests and minor bug fix.