fix lots of bugs based on test