Revert "Revert "fixed some bugs""

This reverts commit eee451e42d7088337406a56232dbbece55f9f55b.
Revert cf66767..6009e5b

This rolls back to commit cf667679d0e7797c01f1e0e012c47684a6b2f20a.
fix bugs