fix some bugs, but still some buffer bugs remain