Fix some bugs.
Refactoring