adjust some logic and fix a test.