Fix some bugs and reorganize