fix some bugs. still more test to do.