fix some bugs. Still to test.