Fixed some bugs, may need more fixing