Revert "Revert "Fixed some bugs""

This reverts commit 2538503edaeb81a72440b595c10691dd697413d5.