Improve design and fix some bugs