fix some bugs and clean code