Rewrote part 2, fixed some bugs