Add tests and bug fix.