Improve database structure and fix some bugs