Add tests and fix bug