Fixed some bugs and reorganized