Add tests. Minor bugfixing