fix verious bugs and make all tests pass