fix some bugs, add some logs, make small functions out of things