General cleanup, including some bad logic