several bugfixes; randomized tests need to be rewritten