Add tests and bugfixes/improvements