Improve the code and fix some bugs.