Fixed some bugs, added some new logic