fix more bugs exposed by tests
only one test fails now