fixed some bugs, some logic errors remaining :/