Fixed some bugs
Revert
Revert "Revert"

This reverts commit 60b81fcb936eea181084d78f64dccfcf0876e6e9.