Revert "Revert "fix bugs""

This reverts commit a2c1a14d6544a68126168a9dca915f0bb8ea9257.