bugfixes and some tests