Revert "Revert "bug fixs""

This reverts commit 9ec127c064915e3430040e4671bf0d3dad1f5a22.