fix a series of bugs