Bugfixes, added some tests