Fixed some bugs pointed out by @jakeonfire