fix several bugs (all unittests should pass now)